mcp
langroid/agent/tools/mcp/init.py
FastMCPClient(server, 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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
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 |
---|---|
str | List[str] | None
|
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
@fastmcp_tool("/path/to/server.py", "get_weather") class WeatherTool: def pretty(self) -> str: return f"Temp is {self.temperature}"
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 |