@@ -235,6 +235,54 @@ calculator_server = MCPServerSSE(
235
235
agent = Agent(' openai:gpt-4o' , toolsets = [weather_server, calculator_server])
236
236
```
237
237
238
+ ## Custom TLS / SSL configuration
239
+
240
+ In some environments you need to tweak how HTTPS connections are established –
241
+ for example to trust an internal Certificate Authority, present a client
242
+ certificate for ** mTLS** , or (during local development only!) disable
243
+ certificate verification altogether.
244
+ All HTTP-based MCP client classes
245
+ ([ ` MCPServerStreamableHTTP ` ] [ pydantic_ai.mcp.MCPServerStreamableHTTP ] and
246
+ [ ` MCPServerSSE ` ] [ pydantic_ai.mcp.MCPServerSSE ] ) expose an ` http_client `
247
+ parameter that lets you pass your own pre-configured
248
+ [ ` httpx.AsyncClient ` ] ( https://www.python-httpx.org/async/ ) .
249
+
250
+ ``` python {title="mcp_custom_tls_client.py" py="3.10"}
251
+ import httpx
252
+ import ssl
253
+
254
+ from pydantic_ai import Agent
255
+ from pydantic_ai.mcp import MCPServerSSE
256
+
257
+
258
+ # Trust an internal / self-signed CA
259
+ ssl_ctx = ssl.create_default_context(cafile = " /etc/ssl/private/my_company_ca.pem" )
260
+
261
+ # OPTIONAL: if the server requires **mutual TLS** load your client certificate
262
+ ssl_ctx.load_cert_chain(certfile = " /etc/ssl/certs/client.crt" , keyfile = " /etc/ssl/private/client.key" ,)
263
+
264
+ http_client = httpx.AsyncClient(
265
+ verify = ssl_ctx,
266
+ timeout = httpx.Timeout(10.0 ),
267
+ )
268
+
269
+ server = MCPServerSSE(
270
+ url = " http://localhost:3001/sse" ,
271
+ http_client = http_client, # (1)!
272
+ )
273
+ agent = Agent(" openai:gpt-4o" , toolsets = [server])
274
+
275
+ async def main ():
276
+ async with agent:
277
+ result = await agent.run(' How many days between 2000-01-01 and 2025-03-18?' )
278
+ print (result.output)
279
+ # > There are 9,208 days between January 1, 2000, and March 18, 2025.
280
+ ```
281
+
282
+ 1 . When you supply ` http_client ` , Pydantic AI re-uses this client for every
283
+ request. Anything supported by ** httpx** (` verify ` , ` cert ` , custom
284
+ proxies, timeouts, etc.) therefore applies to all MCP traffic.
285
+
238
286
## MCP Sampling
239
287
240
288
!!! info "What is MCP Sampling?"
0 commit comments