Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion docs/mcp/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ The MCP servers provide the ability to set a `process_tool_call` which allows
the customisation of tool call requests and their responses.

A common use case for this is to inject metadata to the requests which the server
call needs.
call needs:

```python {title="mcp_process_tool_call.py"}
from typing import Any
Expand Down Expand Up @@ -204,6 +204,40 @@ async def main():
#> {"echo_deps":{"echo":"This is an echo message","deps":42}}
```

How to access the metadata is MCP server SDK specific. For example with the [MCP Python
SDK](https://github.com/modelcontextprotocol/python-sdk), it is accessible via the
[`ctx: Context`](https://github.com/modelcontextprotocol/python-sdk#context)
argument that can be included on tool call handlers:

```python {title="mcp_server.py"}
from typing import Any

from mcp.server.fastmcp import Context, FastMCP
from mcp.server.session import ServerSession

mcp = FastMCP('Pydantic AI MCP Server')
log_level = 'unset'


@mcp.tool()
async def echo_deps(ctx: Context[ServerSession, None]) -> dict[str, Any]:
"""Echo the run context.

Args:
ctx: Context object containing request and session information.

Returns:
Dictionary with an echo message and the deps.
"""
await ctx.info('This is an info message')

deps: Any = getattr(ctx.request_context.meta, 'deps')
return {'echo': 'This is an echo message', 'deps': deps}

if __name__ == '__main__':
mcp.run()
```

## Using Tool Prefixes to Avoid Naming Conflicts

When connecting to multiple MCP servers that might provide tools with the same name, you can use the `tool_prefix` parameter to avoid naming conflicts. This parameter adds a prefix to all tool names from a specific server.
Expand Down