jianmu.mcp¶
适用对象:MCP 集成开发者 / 平台维护者
是否必读:按需
相关模块:jianmu.tool, jianmu.swarm
1. 模块职责¶
jianmu.mcp 提供对 MCP server / client / provider 的公开封装,让外部工具源可以被 jianmu 运行时消费。
它解决的是“把 MCP 工具接入 jianmu”这个问题,而不是本地内置工具声明问题。
2. 适合查什么¶
- 服务端配置与客户端:
MCPServerConfig、MCPClient - MCP 工具封装:
MCPTool、MCPResourceTool - provider 与 server 构造:
MCPToolProvider、MCPServerBuilder
3. 使用建议¶
- 需要消费外部 MCP 工具时,从这里开始
- 如果只是写普通 Python 工具,应优先看
jianmu.tool
4. 最小示例¶
from jianmu.mcp import MCPClient, MCPServerConfig
config = MCPServerConfig(
command="npx",
args=["-y", "@modelcontextprotocol/server-filesystem"],
)
client = MCPClient(config)
5. 常见入口¶
- 想连 MCP server:看
MCPClient - 想把 MCP 工具接进 tool provider:看
MCPToolProvider - 想搭建 MCP server:看
MCPServer、MCPServerBuilder
6. 注意事项¶
- 如果只是写普通 Python 工具,优先回到
jianmu.tool - MCP 的实际可用性依赖外部 server 进程和对应配置
- 这层解决的是“接入外部工具生态”,不是替代本地工具抽象
7. API 参考¶
mcp
¶
Model Context Protocol (MCP) clients, providers, and server integrations for Jianmu.
MCPServerConfig
dataclass
¶
Configuration for starting a subprocess stdio MCP server.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
command |
str
|
Executable name or absolute path used to start the MCP server. |
args |
List[str]
|
Positional arguments passed to the server process. |
env |
Optional[Dict[str, str]]
|
Optional environment variables merged into the child process. |
MCPClient
¶
MCPClient(
server_source: Union[
MCPServerConfig,
str,
List[str],
FastMCP,
Dict[str, Any],
],
server_args: Optional[List[str]] = None,
transport_type: Optional[str] = None,
env: Optional[Dict[str, str]] = None,
**transport_kwargs: Any,
)
MCP client with multi-transport support (fastmcp v2).
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
server_args |
Extra positional arguments appended to subprocess launches. |
|
transport_type |
Optional transport override such as |
|
env |
Environment variables merged into subprocess transports. |
|
transport_kwargs |
Extra transport-specific keyword arguments. |
|
server_source |
Normalized FastMCP server or transport source. |
|
client |
Optional[Client]
|
Connected FastMCP client instance, if any. |
_context_manager |
Async context manager used for connection lifecycle. |
Normalize server config and prepare a lazy MCP client.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
server_source
|
Union[MCPServerConfig, str, List[str], FastMCP, Dict[str, Any]]
|
Server definition to connect to. This may be a
|
必需 |
server_args
|
Optional[List[str]]
|
Extra positional arguments appended to stdio/python server launches. |
None
|
transport_type
|
Optional[str]
|
Optional transport override such as |
None
|
env
|
Optional[Dict[str, str]]
|
Optional environment variables merged into subprocess transports. |
None
|
transport_kwargs
|
Any
|
Extra transport-specific keyword arguments forwarded to the underlying FastMCP transport object. |
{}
|
源代码位于: jianmu/mcp/client.py
connect
async
¶
Open the MCP transport and cache the connected client.
返回:
| 类型 | 描述 |
|---|---|
Any
|
The connected Client instance. |
引发:
| 类型 | 描述 |
|---|---|
Exception
|
If connection to the MCP server fails. |
源代码位于: jianmu/mcp/client.py
close
async
¶
Close the MCP connection and cleanup subprocess transport.
This method handles the cleanup of subprocess transports to avoid 'Event loop is closed' errors during garbage collection.
源代码位于: jianmu/mcp/client.py
list_tools
async
¶
List MCP tools exposed by the connected server.
返回:
| 类型 | 描述 |
|---|---|
List[Any]
|
The resulting |
源代码位于: jianmu/mcp/client.py
list_resources
async
¶
List MCP resources exposed by the connected server.
返回:
| 类型 | 描述 |
|---|---|
List[Any]
|
The resulting |
源代码位于: jianmu/mcp/client.py
read_resource
async
¶
Read one MCP resource by URI.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
uri
|
str
|
The resource URI to read. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Any
|
The resulting |
call_tool
async
¶
Invoke one MCP tool with optional arguments.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
name
|
str
|
The name of the tool to invoke. |
必需 |
arguments
|
Optional[Dict[str, Any]]
|
Optional dictionary of arguments for the tool call. Defaults to None. |
None
|
返回:
| 类型 | 描述 |
|---|---|
Any
|
The resulting |
源代码位于: jianmu/mcp/client.py
list_prompts
async
¶
List MCP prompts exposed by the connected server.
返回:
| 类型 | 描述 |
|---|---|
List[Any]
|
The resulting |
源代码位于: jianmu/mcp/client.py
get_prompt
async
¶
Fetch one MCP prompt definition by name.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
name
|
str
|
The name of the prompt. |
必需 |
arguments
|
Optional[Dict[str, str]]
|
Optional dictionary of arguments. Defaults to None. |
None
|
返回:
| 类型 | 描述 |
|---|---|
Any
|
The resulting |
源代码位于: jianmu/mcp/client.py
ping
async
¶
Ping the MCP server and report connectivity.
返回:
| 类型 | 描述 |
|---|---|
bool
|
True if the ping succeeded, False otherwise. |
源代码位于: jianmu/mcp/client.py
get_transport_info
¶
Return human-readable transport diagnostics for the current client.
返回:
| 类型 | 描述 |
|---|---|
Dict[str, Any]
|
A dictionary containing transport status and diagnostics. |
源代码位于: jianmu/mcp/client.py
as_tools
async
¶
Wrap MCP tool definitions as Jianmu tools.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
allowlist
|
Optional[List[str]]
|
Optional list of tool names to include. If None, all tools are wrapped. |
None
|
返回:
| 类型 | 描述 |
|---|---|
List[MCPTool]
|
A list of wrapped MCPTool objects. |
源代码位于: jianmu/mcp/client.py
as_resource_tools
async
¶
Wrap MCP resources as read-only Jianmu tools.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
allowlist
|
Optional[List[str]]
|
Optional list of resource URIs to include. If None, all resources are wrapped. |
None
|
返回:
| 类型 | 描述 |
|---|---|
List[MCPResourceTool]
|
A list of wrapped MCPResourceTool objects. |
源代码位于: jianmu/mcp/client.py
MCPTool
¶
Bases: Tool
Wrapper that exposes an MCP tool definition as a standard Jianmu Tool.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
_client |
MCP client used to invoke the wrapped tool. |
|
_tool_def |
Raw MCP tool definition returned by the server. |
|
name |
Public tool name exposed to Jianmu callers. |
|
description |
Human-readable tool description. |
|
input_schema |
JSON schema describing accepted inputs. |
|
output_schema |
JSON schema describing tool outputs. |
Wrap one MCP tool definition as a Jianmu tool.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
client
|
MCPClient
|
Connected or lazily connecting MCP client used to invoke the tool. |
必需 |
tool_def
|
Any
|
Raw MCP tool definition returned by the server. |
必需 |
源代码位于: jianmu/mcp/client.py
get_description
¶
Return the MCP-provided tool description instead of the base docstring.
返回:
| 类型 | 描述 |
|---|---|
str
|
The tool description string. |
run
async
¶
Invoke the wrapped MCP tool.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
input
|
Any
|
Single argument payload, or dictionary of arguments. Defaults to None. |
None
|
kwargs
|
Any
|
Named arguments for the tool call. |
{}
|
返回:
| 类型 | 描述 |
|---|---|
Any
|
The tool execution result, or an error string. |
源代码位于: jianmu/mcp/client.py
MCPResourceTool
¶
Bases: Tool
Read-only Tool wrapper for MCP resources.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
_client |
MCP client used to read the wrapped resource. |
|
_resource_def |
Raw MCP resource definition returned by the server. |
|
name |
Public tool name exposed to Jianmu callers. |
|
description |
Human-readable resource description. |
|
input_schema |
Schema for optional URI override input. |
|
output_schema |
Schema describing returned resource content. |
Wrap one MCP resource definition as a read-only tool.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
client
|
MCPClient
|
Connected or lazily connecting MCP client used to read the resource. |
必需 |
resource_def
|
Any
|
Raw MCP resource definition returned by the server. |
必需 |
源代码位于: jianmu/mcp/client.py
run
async
¶
Read the wrapped MCP resource and return its content.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
input
|
Any
|
Resource URI override (optional). |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Any
|
The resource content, or an error string. |
源代码位于: jianmu/mcp/client.py
MCPToolProvider
¶
Bases: BaseToolProvider
Load MCP tools from config and expose them as Jianmu tools.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
config_path |
Filesystem path to the MCP config file. |
|
_clients |
dict[str, MCPClient]
|
Connected MCP clients keyed by configured server name. |
_tools |
list[Tool]
|
Cached wrapped Tool instances exported by all connected servers. |
Create a provider backed by an MCP config file.
源代码位于: jianmu/mcp/provider.py
initialize
async
¶
Connect configured MCP clients eagerly.
源代码位于: jianmu/mcp/provider.py
get_tools
¶
Return cached MCP-backed Jianmu tools.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
_
|
Any
|
Unused keyword arguments. |
{}
|
返回:
| 类型 | 描述 |
|---|---|
list[Tool]
|
A list of cached Tool instances. |
close
async
¶
Close all managed MCP clients.
源代码位于: jianmu/mcp/provider.py
MCPServer
¶
Thin wrapper around FastMCP for convenience.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
mcp |
Underlying FastMCP server instance. |
|
name |
Public server name exposed over the MCP protocol. |
|
description |
Human-readable server description. |
Create a thin FastMCP-backed server wrapper.
源代码位于: jianmu/mcp/server.py
add_tool
¶
Register one tool implementation with the server.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
func
|
Callable
|
The Python function that implements the tool. |
必需 |
name
|
Optional[str]
|
Optional explicit tool name override. Defaults to None. |
None
|
description
|
Optional[str]
|
Optional explicit tool description override. Defaults to None. |
None
|
源代码位于: jianmu/mcp/server.py
add_resource
¶
add_resource(
func: Callable,
uri: Optional[str] = None,
name: Optional[str] = None,
description: Optional[str] = None,
)
Register one MCP resource handler.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
func
|
Callable
|
The Python function that resolves the resource content. |
必需 |
uri
|
Optional[str]
|
Optional resource URI template. Defaults to None. |
None
|
name
|
Optional[str]
|
Optional resource name. Defaults to None. |
None
|
description
|
Optional[str]
|
Optional resource description. Defaults to None. |
None
|
源代码位于: jianmu/mcp/server.py
add_prompt
¶
Register one MCP prompt handler.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
func
|
Callable
|
The Python function that formats the prompt. |
必需 |
name
|
Optional[str]
|
Optional explicit prompt name. Defaults to None. |
None
|
description
|
Optional[str]
|
Optional prompt description. Defaults to None. |
None
|
源代码位于: jianmu/mcp/server.py
run
¶
Start serving the MCP server with FastMCP.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
transport
|
str
|
Subprocess transport style, e.g., 'stdio' or 'sse'. Defaults to 'stdio'. |
'stdio'
|
kwargs
|
Any
|
Extra parameters passed to FastMCP.run. |
{}
|
源代码位于: jianmu/mcp/server.py
get_info
¶
Return a compact description of registered MCP capabilities.
返回:
| 类型 | 描述 |
|---|---|
Dict[str, Any]
|
A dictionary containing server name, description, and protocol info. |
源代码位于: jianmu/mcp/server.py
MCPServerBuilder
¶
Chainable MCP server builder.
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
server |
The MCPServer instance being configured. |
Create a chainable builder for one MCP server.
源代码位于: jianmu/mcp/server.py
with_tool
¶
with_tool(
func: Callable,
name: Optional[str] = None,
description: Optional[str] = None,
) -> MCPServerBuilder
Register one tool and return the builder.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
func
|
Callable
|
The Python function that implements the tool. |
必需 |
name
|
Optional[str]
|
Optional tool name override. Defaults to None. |
None
|
description
|
Optional[str]
|
Optional tool description. Defaults to None. |
None
|
返回:
| 类型 | 描述 |
|---|---|
MCPServerBuilder
|
The resulting |
源代码位于: jianmu/mcp/server.py
with_resource
¶
with_resource(
func: Callable,
uri: Optional[str] = None,
name: Optional[str] = None,
description: Optional[str] = None,
) -> MCPServerBuilder
Register one resource and return the builder.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
func
|
Callable
|
The Python function that resolves the resource content. |
必需 |
uri
|
Optional[str]
|
Optional resource URI. Defaults to None. |
None
|
name
|
Optional[str]
|
Optional resource name. Defaults to None. |
None
|
description
|
Optional[str]
|
Optional resource description. Defaults to None. |
None
|
返回:
| 类型 | 描述 |
|---|---|
MCPServerBuilder
|
The resulting |
源代码位于: jianmu/mcp/server.py
with_prompt
¶
with_prompt(
func: Callable,
name: Optional[str] = None,
description: Optional[str] = None,
) -> MCPServerBuilder
Register one prompt and return the builder.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
func
|
Callable
|
The Python function that formats the prompt. |
必需 |
name
|
Optional[str]
|
Optional prompt name. Defaults to None. |
None
|
description
|
Optional[str]
|
Optional prompt description. Defaults to None. |
None
|
返回:
| 类型 | 描述 |
|---|---|
MCPServerBuilder
|
The resulting |
源代码位于: jianmu/mcp/server.py
build
¶
run
¶
Run the configured server immediately.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
transport
|
str
|
Stdio or SSE transport name. Defaults to 'stdio'. |
'stdio'
|
kwargs
|
Any
|
Additional run options. |
{}
|