纽带
用于构建自己的MCP(模型上下文协议)服务器的模板。以此为起点,创建人工智能助手可以使用的自定义工具。
什么是MCP?
MCP(模型上下文协议)是一个开放协议,规范了人工智能应用程序如何连接到外部工具和数据源。它基于JSON-RPC 2.0构建,使Claude和ChatGPT等LLM主机能够发现和调用服务器公开的工具。
运作原理
┌─────────────────┐ JSON-RPC 2.0 ┌─────────────────┐
│ AI Assistant │ ◄──────────────────► │ MCP Server │
│ (Claude, GPT) │ SSE Transport │ (Your Tools) │
└─────────────────┘ └─────────────────┘
│ │
│ 1. tools/list │
│ ──────────────────────────────────────► │
│ │
│ 2. Returns available tools │
│ ◄────────────────────────────────────── │
│ │
│ 3. tools/call (with arguments) │
│ ──────────────────────────────────────► │
│ │
│ 4. Returns result │
│ ◄────────────────────────────────────── │关键概念
- 工具:AI可以调用的函数(例如。,
web_search,get_writing_guidelines) - 运输:通信层(SSE用于远程服务器,STDIO用于本地)
- JSON 模式:工具使用JSON模式定义其参数以进行验证
快速开始
# Install dependencies
pip install -r requirements.txt
# Set up environment variables
cp .env.example .env
# Edit .env with your API keys
# Run the server
python src/agenticnexus/mcp_server.py服务器运行于 http://localhost:8000 SSE运输。
项目结构
src/agenticnexus/
├── mcp_server.py # Server entry point
├── tools/
│ ├── __init__.py # Tool registry
│ ├── search/ # Web search tool (example)
│ │ ├── __init__.py
│ │ └── utils.py
│ └── writing_style/ # Writing guidelines tool (example)
│ └── __init__.py添加您自己的工具
- 在下创建新文件夹
tools/:
tools/
└── my_tool/
└── __init__.py- 定义您的工具
@mcp.tool():
from mcp.server.fastmcp import FastMCP
def register(mcp: FastMCP) -> None:
@mcp.tool()
async def my_tool(param1: str, param2: int = 10) -> dict:
"""Description of what the tool does."""
# Your logic here
return {"result": "..."}- 注册
tools/__init__.py:
from . import search, writing_style, my_tool
def register_all_tools(mcp: FastMCP) -> None:
search.register(mcp)
writing_style.register(mcp)
my_tool.register(mcp)包含示例工具
| 工具 | 说明 |
|---|---|
web_search | 使用并行API搜索web |
get_writing_guidelines | 获取企业写作风格指南 |
环境变量
| 变量 | 描述 |
|---|---|
PARALLEL_API_KEY | 用于并行web搜索的API密钥 |
连接客户
克劳德桌面
增添 ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)或 %APPDATA%\Claude\claude_desktop_config.json (Windows):
{
"mcpServers": {
"agenticnexus": {
"url": "http://localhost:8000/sse"
}
}
}OpenAI API
response = client.responses.create(
model="gpt-4.1",
tools=[{
"type": "mcp",
"server_label": "agenticnexus",
"server_url": "http://localhost:8000/sse",
"require_approval": "never"
}],
input="Search for latest AI news"
)无烟煤API
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
mcp_servers=[{
"type": "url",
"url": "http://localhost:8000/sse",
"name": "agenticnexus"
}],
messages=[{"role": "user", "content": "Search for latest AI news"}]
)了解更多
许可证
麻省理工学院
