Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

MCP client using httpx.client is closed after one request #2502

Open
Assignees
Labels
Stale questionFurther information is requested
@zhezh

Description

Initial Checks

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

Metadata

Metadata

Assignees

Labels

Stale questionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      AltStyle によって変換されたページ (->オリジナル) /