LangChain代理MCP服务器
一个生产就绪的MCP服务器,通过部署在Google Cloud Run上的模型上下文协议公开LangChain代理功能。
](https://github.com/mcpmessenger/LangchainMCP) ](https://github.com/mcpmessenger/LangchainMCP) 
🚀 概述
这是一个 独立后端服务 它将LangChain代理封装为单个高级MCP工具。服务器由以下组件构建 快速API 并部署在 谷歌云运行,提供可扩展的、生产就绪的解决方案,用于将AI代理功能暴露给任何符合MCP的客户端。
实时服务: https://langchain-agent-mcp-server-554655392699.us-central1.run.app
✨ 特性
- ✅ MCP合规性 -完整的模型上下文协议支持
- ✅ 朗链代理 -基于ReAct模式的多步推理
- ✅ 剧作家沙盒 -可访问性快照的交互式预览(新增!)
- ✅ 谷歌云运行 -可扩展的无服务器部署
- ✅ 工具支持 -自定义工具的可扩展框架
- ✅ 生产就绪 -错误处理、日志记录和监控
- ✅ Docker支持 -容器化,便于部署
🏗️ 建筑
| 组件 | 技术 | 目的 |
|---|---|---|
| 后端框架 | FastAPI | 高性能异步web服务器 |
| 代理框架 | LangChain | 多步推理和工具执行 |
| 部署 | Google Cloud Run | 无服务器、自动扩展托管 |
| 容器化 | Docker | 一致的部署环境 |
| 协议 | 模型上下文协议(MCP) | 标准化工具和上下文共享 |
🛠️ 快速开始
先决条件
- Python 3.11+
- OpenAI API密钥
- Google Cloud帐户(用于Cloud Run部署)
- Docker(可选,用于本地测试)
本地开发
- 克隆存储库:
git clone https://github.com/mcpmessenger/LangchainMCP.git
cd LangchainMCP- 安装依赖项:
# Windows
py -m pip install -r requirements.txt
# Linux/Mac
pip install -r requirements.txt- 设置环境变量:
创建 .env 文件:
OPENAI_API_KEY=your-openai-api-key-here
OPENAI_MODEL=gpt-4o-mini
PORT=8000- 运行服务器:
# Windows
py run_server.py
# Linux/Mac
python run_server.py- 测试端点:
- 健康:http://localhost:8000/health - 显示:http://localhost:8000/mcp/manifest - API文件:http://localhost:8000/docs - 剧作家沙盒:http://localhost:8080/sandbox(启动前端后)
- 启动前端(可选):
# Install frontend dependencies (first time only)
npm install
# Start frontend dev server
npm run dev然后访问http://localhost:8080/sandbox使用Playwright沙盒预览功能。
☁️ 谷歌云运行部署
该服务器设计用于部署 谷歌云运行。请参阅我们的全面部署指南:
- DEPLOY_LOUD_RUN_WINDOWS.md -Windows部署指南
- DEPLOY_LOUD_RUN.md -一般部署指南
- QUICK_DEPLOY.md -快速参考
快速部署
# Windows PowerShell
.\deploy-cloud-run.ps1 -ProjectId "your-project-id" -Region "us-central1"
# Linux/Mac
./deploy-cloud-run.sh your-project-id us-central1当前部署
- 服务URL: https://langchain-agent-mcp-server-554655392699.us-central1.run.app
- 项目: 俚语
- 地区: 美国中央1
- 状态: ✅ 实时和可操作
📡 API终点
MCP端点
获取清单
GET /mcp/manifest返回声明可用工具的MCP清单。
答复:
{
"name": "langchain-agent-mcp-server",
"version": "1.0.0",
"tools": [
{
"name": "agent_executor",
"description": "Execute a complex, multi-step reasoning task...",
"inputSchema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The user's query or task"
},
"system_instruction": {
"type": "string",
"description": "Optional system-level instructions to customize agent behavior"
}
},
"required": ["query"]
}
}
]
}调用工具
POST /mcp/invoke
Content-Type: application/json
{
"tool": "agent_executor",
"arguments": {
"query": "What is the capital of France?",
"task_id": "optional-workflow-id"
}
}使用系统说明(可选):
POST /mcp/invoke
Content-Type: application/json
{
"tool": "agent_executor",
"arguments": {
"query": "Analyze Tesla stock",
"system_instruction": "You are a financial analyst. Provide detailed analysis with specific numbers."
}
}答复:
{
"content": [
{
"type": "text",
"text": "The capital of France is Paris."
}
],
"isError": false
}系统说明
这 agent_executor 工具支持可选 system_instruction 参数,允许您在每次调用的基础上自定义代理的行为。
用途:
- 基本查询 (使用默认提示):
{
"tool": "agent_executor",
"arguments": {
"query": "What is the weather today?"
}
}- 使用自定义指令查询:
{
"tool": "agent_executor",
"arguments": {
"query": "Explain quantum computing",
"system_instruction": "You are a physics professor. Explain concepts clearly and use examples."
}
}- 个性化定制:
{
"tool": "agent_executor",
"arguments": {
"query": "Tell me about space",
"system_instruction": "You are a pirate explaining complex topics. Use pirate terminology!"
}
}笔记:
- 如果
system_instruction如果省略,代理将使用其默认提示 - 忽略空或仅空白的指令(使用默认提示)
- 每次使用自定义指令的调用都会创建一个新的代理实例
剧作家沙盒端点
生成辅助功能快照
POST /api/playwright/snapshot
Content-Type: application/json
{
"url": "wikipedia.org",
"use_cache": true
}答复:
{
"snapshot": "[body]\n Name: Wikipedia\n [main]\n Name: Main content...",
"url": "https://wikipedia.org",
"cached": false,
"token_count": 3307
}特征:
- 生成任何网站的结构化可访问性快照
- 展示人工智能如何通过结构化数据“查看”网站
- 对热门网站的缓存支持
- 令牌计数估计
- 与Windows兼容(使用ProactorEventLoop)
快照测试提示
POST /api/playwright/test-prompt
Content-Type: application/json
{
"snapshot": "[body]\n [button]\n Name: Login",
"prompt": "Find the login button"
}答复:
{
"matches": [
{
"line": 2,
"content": "[button] Name: Login",
"context": "..."
}
],
"prompt": "Find the login button",
"total_matches": 1
}剧作家沙盒用户界面: 访问 http://localhost:8080/sandbox 要使用交互式预览功能,请执行以下操作:
- 输入任何URL以生成快照
- 与AI辅助功能快照并排查看实时网站
- 在快照中查找元素的测试提示
- 查看与完整HTML/屏幕截图相比的令牌节省
其他终点
GET /-服务器信息GET /health-健康检查GET /api/tasks-安全任务摘要(可选监控)GET /api/tasks/{task_id}-安全任务摘要(可选监控)GET /docs-交互式API文档(Swagger UI)
🔧 配置
环境变量
| 变量 | 描述 | 默认值 | 必填 |
|---|---|---|---|
OPENAI_API_KEY | OpenAI API密钥 | - | ✅ 是的 |
OPENAI_MODEL | 要使用的OpenAI模型 | gpt-4o-mini | 没有 |
PORT | 服务器端口 | 8000 | 没有 |
API_KEY | 用于身份验证的可选API密钥 | - | 否 |
MAX_ITERATIONS | 最大代理迭代次数 | 10 | 没有 |
DEFAULT_SYSTEM_INSTRUCTION | 默认系统提示(Glazyr) | - | 否 |
VERBOSE | 启用详细日志记录 | false | 没有 |
POLICY_ENFORCEMENT | 强制执行/mcp/调用策略门 | false | 没有 |
MAX_QUERY_CHARS | 最大允许查询大小 | 5000000 | 没有 |
ALLOWLISTED_DOMAINS | 逗号分隔的域分配列表(查询URL) | - | 否 |
REDIS_URL | 启用Redis状态存储+任务监控 | - | 否 |
TASK_TTL_SECONDS | 任务摘要TTL | 86400 | 没有 |
RECENT_TASKS_MAX | 最近的任务索引大小 | 200 | 没有 |
📚 文档
📖 完整文档网站 -完整的文档和示例(GitHub页面)
快速链接:
在本地构建文档:
# Windows
.\build-docs.ps1 serve
# Linux/Mac
./build-docs.sh serve附加指南:
- README_BACKEND.md -完整的技术文档
- PLAYWRIGHT_SANDBOX_SETUP.md -剧作家沙盒设置和使用
- BUG报告_布局权_未执行错误.md -Windows兼容性修复程序文档
- DEPLOY_LOUD_RUN_WINDOWS.md -Windows部署指南
- 安装_需求分析.md -先决条件安装
- SLASHMCP_INTEGRATION.md -SlashMCP集成指南
- docs/glazyr-integration.md -Glazyr集成说明(截图→ MCP调用)
🧪 测试
# Test health endpoint
Invoke-WebRequest -Uri "https://langchain-agent-mcp-server-554655392699.us-central1.run.app/health"
# Test agent invocation
$body = @{
tool = "agent_executor"
arguments = @{
query = "What is 2+2?"
}
} | ConvertTo-Json
Invoke-WebRequest -Uri "https://langchain-agent-mcp-server-554655392699.us-central1.run.app/mcp/invoke" `
-Method POST `
-ContentType "application/json" `
-Body $body
# Test with system instruction
$bodyWithInstruction = @{
tool = "agent_executor"
arguments = @{
query = "What is 2+2?"
system_instruction = "You are a math teacher. Explain your reasoning step by step."
}
} | ConvertTo-Json
Invoke-WebRequest -Uri "https://langchain-agent-mcp-server-554655392699.us-central1.run.app/mcp/invoke" `
-Method POST `
-ContentType "application/json" `
-Body $bodyWithInstruction🎭 剧作家沙盒功能
Playwright Sandbox是一个交互式预览功能,演示了AI代理如何通过结构化的可访问性数据“查看”网站。此功能对于理解结构化快照与完整HTML或屏幕截图相比的价值特别有用。
特性
- 双视图界面:查看实时网站及其结构化可访问性快照
- 代币效率:比较令牌计数-快照通常比完整HTML小90%以上
- 交互式测试:测试提示以查找快照中的元素
- 缓存:缓存热门网站以获得更快的演示结果
- Windows兼容:已修复
NotImplementedError在Windows上使用ProactorEventLoop
快速开始
- 安装剧作家:
py -m pip install playwright
py -m playwright install chromium- 启动后端:
py run_server.py- 启动前端:
npm install # First time only
npm run dev- 访问沙盒:
打开http://localhost:8080/sandbox并尝试以下URL:
- wikipedia.org - github.com - google.com
运作原理
- 输入URL -系统使用Playwright导航到网站
- 生成快照 -提取结构化的可访问性信息(角色、名称、描述)
- 查看比较 -查看实时网站与人工智能的结构化视图
- 测试提示 -试着让人工智能找到特定的元素
技术细节
- 后端:具有Playwright集成的FastAPI端点
- 前端:使用TanStack查询的React+Vite
- 事件循环:在Windows上使用ProactorEventLoop来支持子进程
- 隐身模式:反机器人检测措施,以提高兼容性
- 错误处理:妥善处理阻止自动访问的网站
看 PLAYWRIGHT_SANDBOX_SETUP.md 有关详细的设置说明。
🏗️ 项目结构
.
├── src/
│ ├── main.py # FastAPI application with MCP endpoints
│ ├── agent.py # LangChain agent definition and tools
│ ├── pages/
│ │ └── Sandbox.tsx # Playwright Sandbox UI component
│ ├── mcp_manifest.json # MCP manifest configuration
│ └── start.sh # Cloud Run startup script
├── tests/
│ └── test_mcp_endpoints.py # Test suite
├── Dockerfile # Container configuration
├── requirements.txt # Python dependencies (includes playwright)
├── deploy-cloud-run.ps1 # Windows deployment script
├── deploy-cloud-run.sh # Linux/Mac deployment script
└── cloudbuild.yaml # Cloud Build configuration🚀 部署选项
Google Cloud Run(推荐)
- 可扩展的 -基于流量的自动缩放
- 无服务器 -只为你使用的东西付费
- 管理 -没有要管理的基础设施
- 快速 -全球CDN的低延迟
看 DEPLOY_LOUD_RUN_WINDOWS.md 详细说明。
Docker(本地/其他平台)
docker build -t langchain-agent-mcp-server .
docker run -p 8000:8000 -e OPENAI_API_KEY=your-key langchain-agent-mcp-server📊 演出
- P95延迟: 对于标准3步ReAct链,\<5秒
- 可扩展性: Cloud Run上的水平扩展
- 正常运行时间: 99.9%目标(云运行SLA)
- 吞吐量: 高效处理并发请求
🔒 安全
- API密钥身份验证(可选)
- 环境变量管理
- Secret Manager集成(云运行)
- 默认情况下为HTTPS(云运行)
- CORS配置
🤝 贡献
我们欢迎捐款!请参阅我们的投稿指南。
- 分叉存储库
- 创建要素分支
- 进行更改
- 提交拉取请求
📜 许可证
该项目根据MIT许可证获得许可。
🔗 链接
- GitHub存储库: https://github.com/mcpmessenger/LangchainMCP
- 实时服务: https://langchain-agent-mcp-server-554655392699.us-central1.run.app
- API文件: https://langchain-agent-mcp-server-554655392699.us-central1.run.app/docs
- 模型上下文协议: https://modelcontextprotocol.io/
🙏 致谢
______________________________________________________________________
状态: ✅ 生产就绪并部署在Google Cloud Run上
