stdio mcp
先写一个工具
点击查看代码
import subprocess
import shlex
from mcp.server.fastmcp import FastMCP
from typing import Annotated
from pydantic import Field, BaseModel
mcp = FastMCP()
@mcp.tool(name="run_shell"
,description="run a shell command"
)
def run_shell_command(
command: Annotated[str, Field(description="shell command will be executed", examples="ls -al")]
, timeout: Annotated[int, Field(description="expired time command faile")]) -> str:
try:
# shell=True 只能执行第一个命令,无法处理数组
ret = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=timeout)
if ret.returncode != 0:
return ret.stderr
return ret.stdout
except Exception as e:
print(e)
if __name__ == "__main__":
mcp.run(transport="stdio")
Client launches server as a subprocess and communicates via standard input/output. Best for local tools and simple setups.
点击查看代码
from langchain_mcp_adapters.client import MultiServerMCPClient
client = MultiServerMCPClient(
{
"给工具起个名字": {
"transport": "stdio",
"command": "python",
"args": ["工具的路径"],
}
}
)
tools = await client.get_tools()
agent = create_agent("claude-sonnet-4-5-20250929", tools)
浙公网安备 33010602011771号