mcp
langroid/agent/tools/mcp/init.py
FastMCPClient(server, persist_connection=False, forward_images=True, forward_text_resources=False, forward_blob_resources=False, sampling_handler=None, roots=None, log_handler=None, message_handler=None, read_timeout_seconds=None)
¶
A client for interacting with a FastMCP server.
Provides async context manager functionality to safely manage resources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
FastMCPServerSpec
|
FastMCP server or path to such a server |
required |
Source code in langroid/agent/tools/mcp/fastmcp_client.py
connect()
async
¶
close()
async
¶
get_tool_async(tool_name)
async
¶
Create a Langroid ToolMessage subclass from the MCP Tool
with the given tool_name.
Source code in langroid/agent/tools/mcp/fastmcp_client.py
tool_model_from_mcp_tool(target)
¶
Build a Langroid ToolMessage subclass from an already-fetched MCP
Tool object.
This is a pure, synchronous, network-free conversion: it performs no
list_tools() round-trip. Pair it with a single list_tools() call
to build many tools without re-listing the server once per tool
(see :meth:get_tools_async).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Tool
|
The raw |
required |
Returns:
| Type | Description |
|---|---|
Type[ToolMessage]
|
A dynamically created Langroid ToolMessage subclass for |
Source code in langroid/agent/tools/mcp/fastmcp_client.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | |
get_tools_async()
async
¶
Get all available tools as Langroid ToolMessage classes,
handling nested schemas, with handle_async methods
Source code in langroid/agent/tools/mcp/fastmcp_client.py
get_mcp_tool_async(name)
async
¶
Find the "original" MCP Tool (i.e. of type mcp.types.Tool) on the server
matching name, or None if missing. This contains the metadata for the tool:
name, description, inputSchema, etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the tool to look up. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Tool]
|
The raw Tool object from the server, or None. |
Source code in langroid/agent/tools/mcp/fastmcp_client.py
call_mcp_tool(tool_name, arguments)
async
¶
Call an MCP tool with the given arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool to call. |
required |
arguments
|
Dict[str, Any]
|
Arguments to pass to the tool. |
required |
Returns:
| Type | Description |
|---|---|
Optional[tuple[str, list[FileAttachment]]]
|
The result of the tool call. |
Source code in langroid/agent/tools/mcp/fastmcp_client.py
mcp_tool(server, tool_name)
¶
Decorator: declare a ToolMessage class bound to a FastMCP tool.
Usage
@mcp_tool("/path/to/server.py", "get_weather") class WeatherTool: def pretty(self) -> str: return f"Temp is {self.temperature}"
The server may be a string/URL/FastMCP/ClientTransport, or a zero-arg
callable returning one of those, e.g. lambda: StdioTransport(...). Using a
factory ensures a fresh transport per connection under fastmcp>=2.13.
Source code in langroid/agent/tools/mcp/decorators.py
get_tool(server, tool_name, **client_kwargs)
¶
Get a single Langroid ToolMessage subclass for a specific MCP tool name (synchronous).
This is a convenience wrapper that creates a temporary FastMCPClient and runs the
async get_tool_async function using asyncio.run().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
FastMCPServerSpec
|
Specification of the FastMCP server to connect to. |
required |
tool_name
|
str
|
The name of the tool to retrieve. |
required |
**client_kwargs
|
Any
|
Additional keyword arguments to pass to the FastMCPClient constructor (e.g., sampling_handler, roots). |
{}
|
Returns:
| Type | Description |
|---|---|
Type[ToolMessage]
|
A dynamically created Langroid ToolMessage subclass representing the |
Type[ToolMessage]
|
requested tool. |
Source code in langroid/agent/tools/mcp/fastmcp_client.py
get_tool_async(server, tool_name, **client_kwargs)
async
¶
Get a single Langroid ToolMessage subclass for a specific MCP tool name (async).
This is a convenience wrapper that creates a temporary FastMCPClient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
FastMCPServerSpec
|
Specification of the FastMCP server to connect to. |
required |
tool_name
|
str
|
The name of the tool to retrieve. |
required |
**client_kwargs
|
Any
|
Additional keyword arguments to pass to the FastMCPClient constructor (e.g., sampling_handler, roots). |
{}
|
Returns:
| Type | Description |
|---|---|
Type[ToolMessage]
|
A dynamically created Langroid ToolMessage subclass representing the |
Type[ToolMessage]
|
requested tool. |
Source code in langroid/agent/tools/mcp/fastmcp_client.py
get_tools(server, **client_kwargs)
¶
Get all available tools as Langroid ToolMessage subclasses (synchronous).
This is a convenience wrapper that creates a temporary FastMCPClient and runs the
async get_tools_async function using asyncio.run().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
FastMCPServerSpec
|
Specification of the FastMCP server to connect to. |
required |
**client_kwargs
|
Any
|
Additional keyword arguments to pass to the FastMCPClient constructor (e.g., sampling_handler, roots). |
{}
|
Returns:
| Type | Description |
|---|---|
List[Type[ToolMessage]]
|
A list of dynamically created Langroid ToolMessage subclasses |
List[Type[ToolMessage]]
|
representing all available tools on the server. |
Source code in langroid/agent/tools/mcp/fastmcp_client.py
get_tools_async(server, **client_kwargs)
async
¶
Get all available tools as Langroid ToolMessage subclasses (async).
This is a convenience wrapper that creates a temporary FastMCPClient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
FastMCPServerSpec
|
Specification of the FastMCP server to connect to. |
required |
**client_kwargs
|
Any
|
Additional keyword arguments to pass to the FastMCPClient constructor (e.g., sampling_handler, roots). |
{}
|
Returns:
| Type | Description |
|---|---|
List[Type[ToolMessage]]
|
A list of dynamically created Langroid ToolMessage subclasses |
List[Type[ToolMessage]]
|
representing all available tools on the server. |
Source code in langroid/agent/tools/mcp/fastmcp_client.py
get_mcp_tool_async(server, name, **client_kwargs)
async
¶
Get the raw MCP Tool object for a specific tool name (async).
This is a convenience wrapper that creates a temporary FastMCPClient to retrieve the tool definition from the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
FastMCPServerSpec
|
Specification of the FastMCP server to connect to. |
required |
name
|
str
|
The name of the tool to look up. |
required |
**client_kwargs
|
Any
|
Additional keyword arguments to pass to the FastMCPClient constructor. |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Tool]
|
The raw |
Optional[Tool]
|
is not found. |
Source code in langroid/agent/tools/mcp/fastmcp_client.py
get_mcp_tools_async(server, **client_kwargs)
async
¶
Get all available raw MCP Tool objects from the server (async).
This is a convenience wrapper that creates a temporary FastMCPClient to retrieve the list of tool definitions from the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
server
|
FastMCPServerSpec
|
Specification of the FastMCP server to connect to. |
required |
**client_kwargs
|
Any
|
Additional keyword arguments to pass to the FastMCPClient constructor. |
{}
|
Returns:
| Type | Description |
|---|---|
List[Tool]
|
A list of raw |