-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Open
Assignees
@zhezh
Description
Initial Checks
- I confirm that I'm using the latest version of Pydantic AI
- I confirm that I searched for my issue in https://github.com/pydantic/pydantic-ai/issues before opening this issue
Description
I use a httpx.AsyncClient to initialize an mcp server,
however, the httpx.AsyncClient is closed after one request/call to the mcp server.
Example code is as below. (The code cannot be executed since the endpoint need sk)
Example Code
from typing import Any import httpx import ssl import asyncio import time from pydantic_ai import Agent, RunContext from pydantic_ai.mcp import MCPServerSSE, MCPServerStreamableHTTP from configs.pydantic_predefined_models import model_dsv3 proxy_socks = 'socks5://xxx:11080' http_client = httpx.AsyncClient( proxy=proxy_socks, timeout=30.0, ) server = MCPServerStreamableHTTP( url='https://mcp.amap.com/mcp?key=xxxxx', http_client=http_client, ) agent = Agent(model_dsv3, toolsets=[server]) async def main(): await agent.run("gps coordinate of Shanghai Minhang") print(http_client.is_closed) # True, The http_client is been closed!! await agent.run("gps coordinate of Shanghai Minhang") ## | File "/usr/local/conda/envs/streamlit/lib/python3.13/site-packages/httpx/_client.py", line 1998, in __aenter__ ## | raise RuntimeError(msg) ## | RuntimeError: Cannot reopen a client instance, once it has been closed. print(http_client.is_closed) if __name__ == "__main__": asyncio.run(main())
Python, Pydantic AI & LLM client version
python 3.13
pydantic_ai 0.6.2