Skip to content

Conversation

Kludex
Copy link
Member

@Kludex Kludex commented Sep 29, 2025

Note

Adds MCPServer.client property to access the underlying ClientSession when running, with docs and tests verifying safe access.

  • MCP Core (pydantic_ai/mcp.py):
    • New API: MCPServer.client property exposes the underlying ClientSession; raises AttributeError if accessed outside the async context.
    • Init refactor: Store ClientSession on self._client immediately and enter it via the exit stack in __aenter__.
  • Docs (docs/mcp/client.md):
    • New section: How to access the underlying client with examples for listing/reading prompts/resources and context-manager caveats.
  • Tests (tests/test_mcp.py):
    • New test: Validates MCPServerStdio.client access behavior (error before context; usable inside), and reading resources.
    • Minor imports added for snapshots and URL types.

Written by Cursor Bugbot for commit dd77803. This will update automatically on new commits. Configure here.

Copy link

Docs Preview

commit: dd77803
Preview URL: https://2efada4d-pydantic-ai-previews.pydantic.workers.dev

@Kludex Kludex requested a review from DouweM September 29, 2025 04:24
@Kludex
Copy link
Member Author

Kludex commented Sep 29, 2025

When I run:

pytest -k 'test_docs_examples[docs/mcp/client.md:630:manual_client_access.py]'

It seems the server doesn't have any resources/prompts at all. Ideas @DouweM ?

@DouweM
Copy link
Collaborator

DouweM commented Sep 29, 2025

@Kludex Code looks good.

The docs examples use a mock MCP server:

class MockMCPServer(AbstractToolset[Any]):
@property
def id(self) -> str | None:
return None # pragma: no cover
async def __aenter__(self) -> MockMCPServer:
return self
async def __aexit__(self, *args: Any) -> None:
pass
async def get_tools(self, ctx: RunContext[Any]) -> dict[str, ToolsetTool[Any]]:
return {}
async def call_tool(
self, name: str, tool_args: dict[str, Any], ctx: RunContext[Any], tool: ToolsetTool[Any]
) -> Any:
return None # pragma: lax no cover

@Kludex Kludex closed this Oct 6, 2025
@Kludex Kludex deleted the access-underlying-client-session-mpc branch October 6, 2025 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants