@@ -47,7 +47,7 @@ class MCPHook(BaseHook):
4747 - **Extra.transport**: Transport type — ``http`` (default), ``sse``, or ``stdio``
4848 - **Extra.command**: Command to run for stdio transport (e.g. ``uvx``)
4949 - **Extra.args**: Command arguments for stdio transport (e.g. ``["mcp-run-python"]``)
50- - **Extra.timeout**: Connection timeout in seconds for stdio (default: 10)
50+ - **Extra.timeout**: Connection init timeout in seconds for stdio (default: 10)
5151
5252 For HTTP/SSE transports the ``Authorization`` header is, by default, a static
5353 ``Bearer`` token taken from the connection ``password``. Endpoints that require
@@ -121,22 +121,27 @@ def _auth_headers(self, conn: Any) -> dict[str, str] | None:
121121
122122 def get_conn (self ) -> Any :
123123 """
124- Return a configured PydanticAI MCP server instance.
124+ Return a configured PydanticAI MCP toolset instance.
125125
126- Creates the appropriate MCP server based on the transport type
127- in the connection's extra field:
126+ Builds a :class:`~pydantic_ai.mcp.MCPToolset` over the FastMCP transport
127+ matching the transport type in the connection's extra field:
128128
129- - ``http`` (default): :class:`~pydantic_ai.mcp.MCPServerStreamableHTTP`
130- - ``sse``: :class:`~pydantic_ai.mcp.MCPServerSSE`
131- - ``stdio``: :class:`~pydantic_ai.mcp.MCPServerStdio`
129+ - ``http`` (default): ``fastmcp.client.transports.StreamableHttpTransport``
130+ - ``sse``: ``fastmcp.client.transports.SSETransport``
131+ - ``stdio``: ``fastmcp.client.transports.StdioTransport``
132+
133+ When ``tool_prefix`` is set the toolset is wrapped via
134+ :meth:`~pydantic_ai.toolsets.abstract.AbstractToolset.prefixed`, so a
135+ prefix of ``"weather"`` yields tool names like ``weather_get_forecast``.
132136
133137 The result is cached for the lifetime of this hook instance.
134138 """
135139 if self ._server is not None :
136140 return self ._server
137141
138142 try :
139- from pydantic_ai .mcp import MCPServerSSE , MCPServerStdio , MCPServerStreamableHTTP
143+ from fastmcp .client .transports import SSETransport , StdioTransport , StreamableHttpTransport
144+ from pydantic_ai .mcp import MCPToolset
140145 except ImportError :
141146 raise ImportError (
142147 'MCP support requires the `mcp` package. Install it with: pip install "pydantic-ai-slim[mcp]"'
@@ -149,15 +154,11 @@ def get_conn(self) -> Any:
149154 if transport == "http" :
150155 if not conn .host :
151156 raise ValueError (f"Connection { self .mcp_conn_id !r} requires a host URL for HTTP transport." )
152- self ._server = MCPServerStreamableHTTP (
153- conn .host , headers = self ._auth_headers (conn ), tool_prefix = self .tool_prefix
154- )
157+ toolset = MCPToolset (StreamableHttpTransport (conn .host , headers = self ._auth_headers (conn )))
155158 elif transport == "sse" :
156159 if not conn .host :
157160 raise ValueError (f"Connection { self .mcp_conn_id !r} requires a host URL for SSE transport." )
158- self ._server = MCPServerSSE (
159- conn .host , headers = self ._auth_headers (conn ), tool_prefix = self .tool_prefix
160- )
161+ toolset = MCPToolset (SSETransport (conn .host , headers = self ._auth_headers (conn )))
161162 elif transport == "stdio" :
162163 command = extra .get ("command" )
163164 if not command :
@@ -168,13 +169,14 @@ def get_conn(self) -> Any:
168169 if isinstance (args , str ):
169170 args = [args ]
170171 timeout = extra .get ("timeout" , 10 )
171- self . _server = MCPServerStdio ( command , args = args , timeout = timeout , tool_prefix = self . tool_prefix )
172+ toolset = MCPToolset ( StdioTransport ( command = command , args = args ), init_timeout = timeout )
172173 else :
173174 raise ValueError (
174175 f"Unknown transport { transport !r} in connection { self .mcp_conn_id !r} . "
175176 "Supported: 'http', 'sse', 'stdio'."
176177 )
177178
179+ self ._server = toolset .prefixed (self .tool_prefix ) if self .tool_prefix else toolset
178180 return self ._server
179181
180182 def test_connection (self ) -> tuple [bool , str ]:
0 commit comments