Skip to content

Feature request: Pass metadata to MCP server tool calls #1872

@alex-panther

Description

@alex-panther

Description

Proposed amendment to the runtool function within _agent_graph.py

model_args = args  # Parameters generated by large language models (LLMs)
client_deps = ctx.deps  # Locally passed client parameters
merged_arguments = {**model_args, **client_deps}  # type: ignore
result = await server.call_tool(tool_name, merged_arguments)  # type: ignore

Key Improvements

  1. ​​Parameter Merging Mechanism​​

Combines LLM-generated arguments (model_args) with local client dependencies (ctx.deps) using dictionary unpacking

  1. ​​Precedence Rule​​

Implements override logic where client_deps values take priority over conflicting model_args

​​3. Context Preservation​​
Maintains type safety through # type: ignore annotations while enabling local parameter propagation to MCP server
Technical Rationale

This modification addresses the current limitation where:

MCP servers receive only LLM-generated parameters

Local context variables (ctx.deps) remain inaccessible at server-side
Client-specific configurations (auth tokens, environment variables) require workarounds

The merged dictionary approach aligns with Pydantic-AI's dependency injection system while maintaining MCP protocol compliance

. Implementation would enhance scenarios like:

ctx.deps = {"api_key": "SECRET_123"} 

Server-side: Directly access merged_arguments['api_key']
Instead of separate auth handling10

References

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions