【万字长文】深入解析8种LLM Agents开发框架:MCP Server全集成实战指南!

发布时间:2026/9/29 23:11:41
【万字长文】深入解析8种LLM Agents开发框架:MCP Server全集成实战指南! 1. 多框架接入 MCP Server 的真实痛点如果你同时维护过两个以上的 LLM Agents 项目大概率遇到过这种局面OpenAI Agents SDK 里写了一套工具注册逻辑换到 LangGraph 要重写一遍再换到 CrewAI 又得改一遍。每个框架对工具的描述格式、调用协议、异步模型都不一样MCP Server 本来是为了统一这件事结果接入层反而成了新的重复劳动。MCPModel Context ProtocolServer 的核心价值是把外部工具搜索、数据库、文件系统、内部 API抽象成一套标准接口让 Agent 通过统一方式调用。它支持 Stdio 和 SSE 两种传输模式前者适合本地开发后者适合服务化部署。但问题在于8 种主流框架对 MCP 的集成方式各不相同有的内置适配器有的需要手写客户端有的干脆只支持工具列表转换。这篇内容面向需要统一接入多框架的开发者。我会先给出 TaoToken 统一 Key/API 通道的配置骨架让你不用为每个框架单独申请和管理密钥然后逐个框架演示 MCP Server 的接入方式和连通性验证动作。目标是一次配置多框架跑通。适合已经写过至少一个 Agent demo、准备把工具层标准化的同学。2. TaoToken 统一 Key 与 API 通道前置配置多框架开发最烦的事情之一是每个框架的模型调用配置分散在不同文件里。OpenAI SDK 用环境变量LangChain 用 ChatOpenAI 参数CrewAI 又有自己的 LLM 配置。一旦要换模型或调整通道得改七八个地方。TaoToken 提供的是 OpenAI 兼容的统一 API 通道一个 Key 可以覆盖多个框架的模型调用需求。你只需要在配置层做一次映射各框架通过读取同一份配置来初始化模型客户端。先拿到 API Key访问 TaoToken API Keys 管理页创建一个新 Key 并保存。注意 Key 只在创建时完整显示一次建议直接写入本地配置文件而不是硬编码在代码里。统一通道的 Base URL 是https://taotoken.net/api兼容 OpenAI 的/v1/chat/completions和/v1/embeddings接口。这意味着任何基于 OpenAI SDK 的框架只需要改base_url和api_key两个参数就能接入。下面给出两个配置骨架config.toml用于 Python 侧统一读取settings.json用于需要 JSON 配置的框架或 IDE 插件。# config.toml - 统一模型通道配置 [llm] provider openai-compatible base_url https://taotoken.net/api api_key sk-your-taotoken-key default_model gpt-4o fallback_model gpt-4o-mini timeout 60 max_retries 3 [llm.models] reasoning gpt-4o fast gpt-4o-mini embedding text-embedding-3-large [mcp] # MCP Server 通用配置 transport stdio connect_timeout 30 tool_call_timeout 120{ llm: { base_url: https://taotoken.net/api, api_key: sk-your-taotoken-key, default_model: gpt-4o }, mcp_servers: { search: { command: npx, args: [-y, modelcontextprotocol/server-brave-search], env: { BRAVE_API_KEY: your-brave-key } }, filesystem: { command: npx, args: [-y, modelcontextprotocol/server-filesystem, ./workspace] } } }注意api_key不要提交到 Git 仓库。建议用.env或系统环境变量注入配置文件里只保留占位符。配置好之后先用一个最小请求验证通道是否通from openai import OpenAI client OpenAI( base_urlhttps://taotoken.net/api, api_keysk-your-taotoken-key ) resp client.chat.completions.create( modelgpt-4o-mini, messages[{role: user, content: 回复 OK 两个字母}] ) print(resp.choices[0].message.content)如果返回OK说明统一通道已经可用。接下来各框架只需要读取这份配置即可。3. 八种框架的 MCP Server 接入配置这一节是核心。我会按框架逐个给出 MCP 接入的关键代码和配置差异点。所有框架共用上一节的 TaoToken 通道不再重复 Key 配置。3.1 OpenAI Agents SDK轻量级工具注册OpenAI Agents SDK 的 MCP 集成走的是MCPServerStdio类工具列表通过list_tools()获取后直接传给 Agent。import asyncio from agents import Agent, Runner from agents.mcp import MCPServerStdio from openai import AsyncOpenAI async def main(): client AsyncOpenAI( base_urlhttps://taotoken.net/api, api_keysk-your-taotoken-key ) async with MCPServerStdio( params{ command: npx, args: [-y, modelcontextprotocol/server-filesystem, ./workspace] } ) as server: tools await server.list_tools() print(f发现工具: {[t.name for t in tools]}) agent Agent( name文件助手, instructions你可以读写工作目录下的文件, mcp_servers[server], modelgpt-4o-mini ) result await Runner.run(agent, 列出当前目录下的所有文件) print(result.final_output) asyncio.run(main())关键点MCPServerStdio用异步上下文管理器管理生命周期退出时自动关闭子进程。工具发现和调用是分离的list_tools()只返回元数据实际调用由 Agent 运行时触发。3.2 LangGraph状态图里的工具节点LangGraph 的 MCP 集成依赖langchain-mcp-adapters把 MCP 工具转换成 LangChain Tool 对象后注入 ReAct Agent。import asyncio from langgraph.prebuilt import create_react_agent from langchain_mcp_adapters.client import MultiServerMCPClient from langchain_openai import ChatOpenAI async def main(): llm ChatOpenAI( base_urlhttps://taotoken.net/api, api_keysk-your-taotoken-key, modelgpt-4o-mini ) client MultiServerMCPClient({ filesystem: { command: npx, args: [-y, modelcontextprotocol/server-filesystem, ./workspace], transport: stdio } }) tools await client.get_tools() print(f加载工具: {[t.name for t in tools]}) agent create_react_agent(llm, tools) result await agent.ainvoke({ messages: [(user, 读取 README.md 的前 10 行)] }) print(result[messages][-1].content) asyncio.run(main())LangGraph 的优势在于可以把 MCP 工具调用嵌入到状态图的任意节点配合条件边实现复杂的工具编排逻辑。3.3 LlamaIndexRAG 与工具混合LlamaIndex 通过McpToolSpec把 MCP 工具包装成ToolMetadata可以和 QueryEngineTool 混用。import asyncio from llama_index.core.agent import ReActAgent from llama_index.core.tools import QueryEngineTool from llama_index.llms.openai_like import OpenAILike from llama_index.tools.mcp import BasicMCPClient, McpToolSpec async def main(): llm OpenAILike( api_basehttps://taotoken.net/api, api_keysk-your-taotoken-key, modelgpt-4o-mini, is_chat_modelTrue ) mcp_client BasicMCPClient(npx, args[ -y, modelcontextprotocol/server-filesystem, ./workspace ]) mcp_spec McpToolSpec(clientmcp_client) mcp_tools await mcp_spec.to_tool_list_async() agent ReActAgent.from_tools( toolsmcp_tools, llmllm, verboseTrue ) resp await agent.achat(统计 workspace 下有多少个 .py 文件) print(resp.response) asyncio.run(main())注意OpenAILike需要显式设置is_chat_modelTrue否则会走 completion 接口导致 404。3.4 AutoGen 0.4分布式 Agent 的工具注入AutoGen 0.4 的 MCP 集成通过autogen-ext里的McpWorkbench实现支持 Stdio 和 SSE 两种传输。import asyncio from autogen_agentchat.agents import AssistantAgent from autogen_ext.models.openai import OpenAIChatCompletionClient from autogen_ext.tools.mcp import McpWorkbench, StdioServerParams async def main(): model_client OpenAIChatCompletionClient( base_urlhttps://taotoken.net/api, api_keysk-your-taotoken-key, modelgpt-4o-mini ) params StdioServerParams( commandnpx, args[-y, modelcontextprotocol/server-filesystem, ./workspace] ) async with McpWorkbench(params) as workbench: agent AssistantAgent( namefile_agent, model_clientmodel_client, workbenchworkbench, system_message你可以操作工作目录下的文件 ) result await agent.run(task列出所有 .md 文件) print(result.messages[-1].content) asyncio.run(main())AutoGen 的 workbench 抽象层比较厚好处是切换传输模式只需要改params类型业务代码不动。3.5 Pydantic AI结构化输出的工具调用Pydantic AI 的 MCP 集成走pydantic_ai.mcp模块工具调用结果可以直接映射到 Pydantic 模型。import asyncio from pydantic import BaseModel from pydantic_ai import Agent from pydantic_ai.mcp import MCPServerStdio from pydantic_ai.models.openai import OpenAIModel class FileInfo(BaseModel): name: str size_bytes: int extension: str class FileList(BaseModel): files: list[FileInfo] total_count: int async def main(): model OpenAIModel( gpt-4o-mini, base_urlhttps://taotoken.net/api, api_keysk-your-taotoken-key ) server MCPServerStdio( npx, args[-y, modelcontextprotocol/server-filesystem, ./workspace] ) agent Agent( model, mcp_servers[server], result_typeFileList, system_prompt列出文件并返回结构化信息 ) async with agent.run_mcp_servers(): result await agent.run(列出 workspace 下所有文件) print(f共 {result.data.total_count} 个文件) for f in result.data.files: print(f {f.name} ({f.size_bytes} bytes)) asyncio.run(main())result_type指定后模型输出会被强制解析成FileList解析失败会自动重试。这是 Pydantic AI 相比其他框架最实用的特性。3.6 SmolAgents代码生成式工具调用SmolAgents 的ToolCollection.from_mcp把 MCP 工具转成可被代码调用的 Python 对象。import asyncio from smolagents import CodeAgent, ToolCollection, OpenAIServerModel from mcp import StdioServerParameters async def main(): model OpenAIServerModel( model_idgpt-4o-mini, api_basehttps://taotoken.net/api, api_keysk-your-taotoken-key ) server_params StdioServerParameters( commandnpx, args[-y, modelcontextprotocol/server-filesystem, ./workspace] ) with ToolCollection.from_mcp(server_params, trust_remote_codeTrue) as tools: agent CodeAgent( tools[*tools], modelmodel, additional_authorized_imports[json, pathlib] ) result await agent.run_async( 统计 workspace 下所有文件的总大小返回字节数 ) print(result) asyncio.run(main())SmolAgents 会生成 Python 代码来调用工具所以需要additional_authorized_imports白名单。生产环境建议限制导入范围。3.7 CrewAI团队协作中的工具共享CrewAI 本身没有内置 MCP 适配器需要手写一个BaseTool包装器。import asyncio from crewai import Agent, Task, Crew from crewai.tools import BaseTool from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client class MCPFileTool(BaseTool): name: str mcp_file_reader description: str 通过 MCP 读取工作目录下的文件内容 def _run(self, path: str) - str: return asyncio.run(self._async_run(path)) async def _async_run(self, path: str) - str: params StdioServerParameters( commandnpx, args[-y, modelcontextprotocol/server-filesystem, ./workspace] ) async with stdio_client(params) as (read, write): async with ClientSession(read, write) as session: await session.initialize() result await session.call_tool( read_file, {path: path} ) return result.content[0].text async def main(): tool MCPFileTool() agent Agent( role文件分析员, goal读取并总结文件内容, backstory你擅长快速提取文件关键信息, tools[tool], llmgpt-4o-mini ) task Task( description读取 README.md 并总结成三句话, expected_output三句话总结, agentagent ) crew Crew(agents[agent], tasks[task]) result crew.kickoff() print(result) asyncio.run(main())CrewAI 的BaseTool._run是同步接口内部用asyncio.run桥接异步 MCP 调用。注意每次调用都会新建连接高频场景建议做连接池。3.8 Camel角色扮演中的工具分配Camel 的MCPToolkit可以把 MCP 工具分配给不同角色的 Agent。import asyncio from camel.agents import ChatAgent from camel.models import ModelFactory from camel.types import ModelPlatformType from camel.toolkits import MCPToolkit async def main(): model ModelFactory.create( model_platformModelPlatformType.OPENAI, model_typegpt-4o-mini, urlhttps://taotoken.net/api, api_keysk-your-taotoken-key ) toolkit MCPToolkit( commandnpx, args[-y, modelcontextprotocol/server-filesystem, ./workspace] ) await toolkit.connect() tools toolkit.get_tools() agent ChatAgent( system_message你是文件管理助手, modelmodel, toolstools ) resp await agent.astep(列出 workspace 下所有 .json 文件) print(resp.msgs[0].content) await toolkit.disconnect() asyncio.run(main())Camel 的 toolkit 生命周期需要手动管理connect()和disconnect()必须配对调用否则子进程会残留。4. 连通性验证与成功结果判读配置写完之后不要急着跑完整业务逻辑。先用一个最小验证脚本确认 MCP Server 能启动、工具能发现、调用能返回。import asyncio from mcp import ClientSession, StdioServerParameters from mcp.client.stdio import stdio_client async def verify_mcp(): params StdioServerParameters( commandnpx, args[-y, modelcontextprotocol/server-filesystem, ./workspace] ) async with stdio_client(params) as (read, write): async with ClientSession(read, write) as session: await session.initialize() tools await session.list_tools() print( 工具列表 ) for t in tools.tools: print(f {t.name}: {t.description[:60]}) result await session.call_tool( list_directory, {path: .} ) print(\n 调用结果 ) print(result.content[0].text[:500]) asyncio.run(verify_mcp())成功时你会看到类似输出 工具列表 read_file: Read complete contents of a file write_file: Create a new file with content list_directory: List files and directories ... 调用结果 [FILE] README.md [FILE] config.toml [DIR] src [DIR] tests如果工具列表为空说明 MCP Server 启动失败或协议版本不匹配。如果调用返回Method not found说明工具名拼写错误或该 Server 不支持此工具。各框架的验证动作可以统一成三步先单独跑 MCP 客户端验证工具可用再把工具注入框架 Agent最后用一句简单指令触发工具调用并检查返回。任何一步失败问题范围就缩小到那一层。5. 本篇常见错误排查5.1 MCP Server 启动超时现象StdioServerParameters初始化后卡住30 秒后抛TimeoutError。原因通常是npx首次下载包太慢或者命令路径不对。解决方式先在终端手动执行一次npx -y modelcontextprotocol/server-filesystem ./workspace确认能正常启动。如果手动能跑但代码里不行检查command是否用了绝对路径以及env是否传递了必要的环境变量。5.2 工具调用返回 401 或 403现象MCP 工具本身能列出但调用时返回鉴权错误。这通常是 MCP Server 依赖的外部 API Key 没传进去。比如 Brave Search Server 需要BRAVE_API_KEY文件系统 Server 需要正确的目录权限。检查StdioServerParameters的env字段确保所有依赖的密钥都传了。5.3 模型返回 404 或 model not found现象Agent 初始化时报模型不存在。TaoToken 通道的模型名要和实际支持的名称一致。gpt-4o、gpt-4o-mini、text-embedding-3-large这些是通用名称。如果你用了自定义别名需要在配置里做映射。另外注意base_url结尾不要带/v1SDK 会自动拼接。5.4 异步事件循环冲突现象RuntimeError: This event loop is already running。CrewAI 和部分同步框架内部用asyncio.run桥接异步 MCP 调用如果外层已经在事件循环里就会冲突。解决方式把同步框架的调用放到独立线程里执行或者改用框架原生的异步接口。5.5 工具调用结果被截断现象读取大文件时只返回前几百字符。MCP 协议对单次响应有大小限制不同 Server 实现不同。文件系统 Server 默认可能截断。解决方式在工具调用参数里指定head或offset分页读取。或者改用支持流式返回的 Server。5.6 多框架共用配置时的 Key 泄漏现象配置文件被提交到仓库Key 暴露。解决方式.gitignore里加上config.toml和settings.json仓库里只保留config.toml.example。CI 环境用 secrets 注入。TaoToken 的 Key 可以在控制台随时吊销重建发现泄漏立即轮换。6. 多框架统一接入的后续动作把 8 种框架的 MCP 接入跑通之后下一步通常是做工具层的抽象。你可以写一个MCPRegistry类统一管理 Server 的启动、工具发现和生命周期各框架只负责把工具列表注入自己的 Agent。这样新增一个 MCP Server 时只需要在注册表里加一条配置所有框架自动可用。如果你还在选型阶段建议先用 模型对话 快速验证模型输出质量确认通道稳定后再接入框架。长期做编码类 Agent 的话Coding Plan 的额度模型更适合高频调用场景。接入过程中遇到协议层问题接入文档 里有各框架的兼容性说明和参数对照表。我自己的做法是本地开发用 Stdio 模式每个框架独立进程预发环境切 SSE 模式MCP Server 单独部署成服务多个 Agent 共享连接。这样工具更新只需要重启 Server不用动 Agent 代码。

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询