集成PayLink的基本MCP服务器
一个简单的例子演示如何构建 MCP(模型上下文协议)服务器 并使用PayLink基础设施将其连接到AI代理。
概述
该项目展示了以下内容之间的整合:
- MCP服务器 -AI模型可以使用的工具
- PayLink -用于将MCP服务器连接到代理的基础架构
- AI智能体 -使用MCP工具的LangChain/LangGraph代理
此示例演示了基本的MCP服务器设置:
- MCP服务器 通过HTTP公开工具(加、减)
- AI 代理 使用连接到MCP服务器
PayLinkTools - PayLink 提供代理和服务器之间的连接基础架构
- 工具执行 并将结果返回给代理
项目结构
basic-mcp+agent/
├── README.md # This file
├── example_mcp_server/ # The MCP server
│ ├── main.py # Server implementation
│ ├── pyproject.toml # Dependencies
│ ├── .env.example # Environment variable template
│ └── README.md
└── agent/ # The AI agent consumer
├── src/
│ └── graph.py # LangGraph agent definition
├── notebooks/
│ └── use_monitized_mcp.ipynb # Interactive example
├── langgraph.json # LangGraph configuration
├── pyproject.toml # Dependencies
├── .env.example # Environment variable template
└── README.md快速开始
先决条件
- Python 3.13+
- 紫外线 -快速Python包管理器
- OpenAI API密钥 -对于AI代理
步骤1:启动MCP服务器
# Create and activate virtual environment
uv venv
source .venv/bin/activate
# Install dependencies
uv sync
# Run the server
uv run main.py服务器将在以下时间启动 http://0.0.0.0:5003/mcp
步骤2:配置AI代理
打开一个新终端:
# Navigate to the agent directory
cd agent
# Copy the environment template
cp .env.example .env
# Edit .env and replace the values with your custom values:
# - WALLET_CONNECTION_STRING (generate on PayLink platform)
# - OPENAI_API_KEY (your OpenAI API key)步骤3:运行AI代理
# Create and activate virtual environment
uv venv
source .venv/bin/activate
# Install dependencies
uv sync
# Run the LangGraph development server
langgraph dev代理人将在 http://127.0.0.1:2024 使用Studio UI。
示例流程
User: "What is 15 plus 27?"
1. Agent receives the query
2. LLM decides to use the "add" tool
3. PayLinkTools sends request to MCP server
- Includes wallet credentials in headers
4. MCP server receives the request
5. Tool executes: 15 + 27 = 42
6. Result returned to agent
7. Agent responds: "15 plus 27 equals 42"定制
添加新工具
- 在中添加工具定义
list_tools() - 在中实现工具逻辑
call_tool()
@app.list_tools()
async def list_tools() -> list[types.Tool]:
return [
# ... existing tools
types.Tool(
name="multiply",
description="Multiply two integers",
inputSchema={
"type": "object",
"properties": {
"a": {"type": "number"},
"b": {"type": "number"},
},
"required": ["a", "b"],
},
),
]
@app.call_tool()
async def call_tool(tool_name: str, arguments: dict[str, Any]) -> list[TextContent]:
# ... existing logic
elif tool_name == "multiply":
result = arguments["a"] * arguments["b"]
return [types.TextContent(type="text", text=str(result))]资源
许可证
此示例作为PayLink SDK的一部分提供用于教育目的。
