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, tool_name_prefix=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 |
tool_name_prefix
|
str | None
|
Optional namespace prepended to generated Langroid
tool names as |
None
|
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
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 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 | |
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 |