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
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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 | |
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 |