🤖 LangChain MCP API
具有多提供商LLM支持的高性能Go实现
](https://golang.org)   ](https://hub.docker.com)
*通用LangChain服务器支持OpenAI、Claude、Ollama、Llama.cpp等,并集成MCP工具*
______________________________________________________________________
✨ 特性
🚀 多个LLM提供商
- OpenAI (GPT-4、GPT-3.5、GPT-4o)
- 克劳德 (人类学)
- 开放路由 (100+型号)
- 奥拉玛 (本地模型)
- Llama.cpp (GGUF型号)
- vLLM (高性能推理)
🔧 高级功能
- MCP工具 -动态刀具加载
- 流媒体 -实时SSE响应
- 代理系统 -自主任务执行
- 上下文管理 -智能历史修剪
- 详细日志 -详细的执行痕迹
______________________________________________________________________
🐳 码头工人
选项1:从Docker Hub拉取(推荐)
从Docker Hub中提取预构建映像:
# Pull the latest image
docker pull jefriherditriyanto/langchain-mcp-api:latest
# Run container
docker run -d \
--name langchain-mcp-api \
-p 6000:6000 \
jefriherditriyanto/langchain-mcp-api:latest使用Docker Compose:
version: '3.8'
services:
langchain-mcp-api:
image: jefriherditriyanto/langchain-mcp-api:latest
container_name: langchain-mcp-api
ports:
- "6000:6000"
environment:
- PORT=6000
restart: unless-stopped运行方式:
docker-compose up -d______________________________________________________________________
📡 API文档
基础URL
http://localhost:6000端点
1️⃣ 你好,世界
GET /答复:
{
"message": "🤖 LangChain MCP API is running",
"version": "1.0.0"
}______________________________________________________________________
2️⃣ 健康检查
GET /health答复:
{
"status": "ok"
}______________________________________________________________________
3️⃣ 聊天(非流媒体)
POST /chat请求正文:
{
"credential": {
"provider": "openai",
"api_key": "sk-...",
"model": "gpt-4o-mini",
"set": {
"temperature": 0.7,
"max_tokens": 2000
}
},
"system_prompt": "You are a helpful assistant",
"input": "tolong cari dns lookup dari www.example.com, sajikan dalam bahasa indonesia",
"servers": ["http://host.docker.internal:4050"] // this tool available on "MCP Server: Python"
}答复:
{
"messages": [
{
"role": "user",
"content": "tolong cari dns lookup dari www.example.com, sajikan dalam bahasa indonesia"
},
{
"role": "assistant",
"content": "{\"tool_name\":\"network_dns_lookup\",\"tool_args\":{\"hostname\":\"www.example.com\"}}",
"tool_calls": [
{
"id": "manual_1770445792732616000",
"name": "network_dns_lookup",
"args": {
"hostname": "www.example.com"
},
"type": "tool_call"
}
],
"response_metadata": {
"finish_reason": "stop",
"model_provider": "openai",
"model_name": "gpt-4o-mini",
"usage": null,
"system_fingerprint": ""
},
"usage_metadata": {
"output_tokens": 120,
"input_tokens": 1382,
"total_tokens": 1502
}
},
{
"role": "tool",
"content": "Tool 'network_dns_lookup' SUCCESS: {\"hostname\":\"www.example.com\",\"ip\":\"104.18.26.120\"}",
"tool_call_id": "manual_1770445792732616000",
"name": "network_dns_lookup"
},
{
"role": "assistant",
"content": "Berikut hasil **DNS lookup** untuk `www.example.com`:\n\n- **Nama Domain**: www.example.com \n- **Alamat IP (IPv4)**: 104.18.26.120 \n\n### Penjelasan:\nDNS (Domain Name System) berfungsi untuk mengubah nama domain (seperti `www.example.com`) menjadi alamat IP (seperti `104.18.26.120`) yang digunakan oleh komputer untuk mengakses situs web.",
"response_metadata": {
"finish_reason": "stop",
"model_provider": "openai",
"model_name": "gpt-4o-mini",
"usage": null,
"system_fingerprint": ""
},
"usage_metadata": {
"output_tokens": 611,
"input_tokens": 84,
"total_tokens": 695
}
}
],
"message": "Berikut hasil **DNS lookup** untuk `www.example.com`:\n\n- **Nama Domain**: www.example.com \n- **Alamat IP (IPv4)**: 104.18.26.120 \n\n### Penjelasan:\nDNS (Domain Name System) berfungsi untuk mengubah nama domain (seperti `www.example.com`) menjadi alamat IP (seperti `104.18.26.120`) yang digunakan oleh komputer untuk mengakses situs web.",
"usage_metadata": {
"output_tokens": 731,
"input_tokens": 1466,
"total_tokens": 2197
},
"model_provider": "openai",
"model_name": "gpt-4o-mini",
"finish_reason": "stop",
"total_iterations": 2,
"tool_calls_count": 1,
"execution_time_ms": 26818,
"execution_time_sec": 26.818,
"tokens_per_second": 81.92
}______________________________________________________________________
4️⃣ 聊天流(SSE)
POST /chat/stream请求正文: *(与 /chat)*
答复: 服务器发送的事件流
data: {"type":"start","timestamp":"2024-02-04T09:00:00Z","input":"What is the weather?"}
data: {"type":"servers_checked","available_servers":["http://host.docker.internal:4000"],"total_servers":1}
data: {"type":"thinking_start","timestamp":"2024-02-04T09:00:01Z"}
data: {"type":"thinking_chunk","chunk":"I need to check the weather...","is_final":false}
data: {"type":"message_start","timestamp":"2024-02-04T09:00:02Z"}
data: {"type":"message_chunk","chunk":"The weather is ","is_final":false}
data: {"type":"message_chunk","chunk":"sunny, 28°C","is_final":true}
data: {"type":"done","done":true,"total_steps":3,"timestamp":"2024-02-04T09:00:03Z"}______________________________________________________________________
⚙️ 配置
提供商设置
| 提供者 | 密钥 | 必填字段 |
|---|---|---|
| OpenAI | openai | api_key, model |
| 克劳德 | claude | api_key, model |
| 开放路由 | openrouter | api_key, model |
| 奥拉玛 | ollama | url, model |
| Llama.cpp | llama_cpp | url, model |
| vLLM | vllm | url, model |
高级设置
{
"set": {
"temperature": 0.7, // Creativity (0.0 - 2.0)
"max_tokens": 1000, // Max response length
"top_p": 0.9, // Nucleus sampling
"frequency_penalty": 0.0, // Repetition penalty
"presence_penalty": 0.0, // Topic diversity
"max_context_messages": 4 // History window size
}
}______________________________________________________________________
� MCP服务器示例
构建您自己的MCP(模型上下文协议)服务器,为LangChain MCP API提供自定义工具。
完整示例
完整的工作示例可在以下网址获得:
- mcp服务器bunts express -BunTS/Express实施
- mcp服务器golang gofiber -Golang/GoFiber实施
- mcp服务器python fastapi -Python/FastAPI实现
- mcp服务器php代码点火器 -PHP(FrankenPHP)/CodeIgniter实现
- mcp服务器php-laravel -PHP(FrankenPHP)/Laravel实现
- 马上就来。..(Rust、Java、Kotlin、C#、Ruby、无服务器、N8N、Node Red)
快速开始
# Clone MCP server example
git clone https://github.com/jefripunza/langchain-mcp-api/tree/master/mcp-server-bunts-express
# Navigate to MCP server example
cd mcp-server-bunts-express
# Install dependencies
bun install
# Run the server
bun run dev服务器将在以下时间启动 http://localhost:4000 🎉
______________________________________________________________________
项目结构
mcp-server-bunts-express/
├── src/
│ ├── index.ts # Main server
│ ├── registry.ts # Tool registry
│ └── tools/
│ ├── math.ts # Math tools
│ └── weather.ts # Weather tools
├── package.json
└── tsconfig.json______________________________________________________________________
实施指南
1️⃣ 主服务器 (src/index.ts)
import express from "express";
import cors from "cors";
import helmet from "helmet";
import morgan from "morgan";
import { tools, findTool } from "./registry";
const app = express();
app.use(express.json());
app.use(cors());
app.use(helmet());
app.listen(4000, () => {
console.log("🧠 MCP Server running on http://localhost:4000");
});
app.use(morgan("dev"));
// REQUIRED!
app.get("/health", (_req, res) => res.json({ status: "ok" }));
// MCP-style: list tools, REQUIRED!
app.get("/mcp/tools", (_req, res) => {
res.json(
tools.map((t) => ({
name: t.name,
description: t.description,
parameters: t.parameters,
})),
);
});
// MCP-style: invoke tool, REQUIRED!
app.post("/mcp/invoke", async (req, res) => {
const { name, arguments: args } = req.body;
const tool = findTool(name);
if (!tool) {
return res.status(404).json({ error: "Tool not found" });
}
if (!tool.handler) {
return res.status(400).json({ error: "Tool handler not found" });
}
const result = await tool.handler(args);
res.json(result);
});______________________________________________________________________
2️⃣ 工具注册表 (src/registry.ts)
import type { Tool } from "./types/tool";
import { mathTools } from "./tools/math";
import { weatherTools } from "./tools/weather";
export const tools: Tool[] = [...mathTools, ...weatherTools];
export function findTool(name: string) {
return tools.find((t) => t.name === name);
}______________________________________________________________________
3️⃣ 数学工具示例 (src/tools/math.ts)
import type { Tool } from "../types/tool";
export const mathTools: Tool[] = [
{
name: "add",
description: "Add two numbers together", // add to main prompt, please detail!
// add to main prompt, please detail!
parameters: {
type: "object",
properties: {
a: { type: "number" },
b: { type: "number" },
},
required: ["a", "b"],
},
// as controller / logic base
handler: async ({ a, b }: { a: number; b: number }) => {
console.log(`✅ MCP1 Math: ${a}+${b}=${a + b}`);
return { result: a + b };
},
},
];______________________________________________________________________
4️⃣ 天气工具示例 (src/tools/weather.ts)
import { fetchWeatherApi } from "openmeteo";
import type { Tool } from "../types/tool";
// advance tool version
export const weatherTools: Tool[] = [
{
name: "getWeather",
description: "Get weather data by coordinates",
parameters: {
type: "object",
properties: {
latitude: { type: "number" },
longitude: { type: "number" },
},
required: ["latitude", "longitude"],
},
handler: async ({
latitude,
longitude,
}: {
latitude: number;
longitude: number;
}) => {
const params = {
latitude,
longitude,
hourly: ["temperature_2m", "relative_humidity_2m", "rain"],
timezone: "auto",
};
const responses = await fetchWeatherApi(
"https://api.open-meteo.com/v1/forecast",
params
);
const response = responses[0];
const hourly = response.hourly()!;
console.log(`✅ MCP1 Weather: ${latitude}, ${longitude}`);
return {
latitude,
longitude,
temperature: hourly.variables(0)!.valuesArray(),
humidity: hourly.variables(1)!.valuesArray(),
rain: hourly.variables(2)!.valuesArray(),
};
},
},
];______________________________________________________________________
MCP协议端点
您的MCP服务器必须实现这三个非常必要的端点:
| 端点 | 方法 | 描述 |
|---|---|---|
/health | GET | 健康检查 |
/mcp/tools | GET | 列出所有可用工具 |
/mcp/invoke | POST | 执行特定工具 |
______________________________________________________________________
测试您的MCP服务器
# List available tools
curl http://localhost:4000/mcp/tools
# Invoke math tool
curl -X POST http://localhost:4000/mcp/invoke \
-H "Content-Type: application/json" \
-d '{
"name": "add",
"arguments": {"a": 5, "b": 3}
}'
# Invoke weather tool
curl -X POST http://localhost:4000/mcp/invoke \
-H "Content-Type: application/json" \
-d '{
"name": "getWeather",
"arguments": {"latitude": -6.2, "longitude": 106.8}
}'______________________________________________________________________
与LangChain MCP API一起使用
MCP服务器运行后,将其与LangChain MCP API一起使用:
curl -X POST http://localhost:6000/chat \
-H "Content-Type: application/json" \
-d '{
"credential": {
"provider": "openai",
"api_key": "sk-...",
"model": "gpt-4o-mini"
},
"input": "What is 25 + 37?",
"servers": ["http://host.docker.internal:4000"]
}'LangChain MCP API将自动:
- 从您的MCP服务器发现工具
- 让LLM决定使用哪些工具
- 执行工具并返回结果
______________________________________________________________________
🔍 详细日志
使用控制详细的执行日志 VERBOSE 环境变量:
启用详细模式
# Enable verbose logging (shows all requestID-prefixed logs)
export VERBOSE=true
go run main.go
# Or with Docker
docker run -d \
-p 6000:6000 \
-e VERBOSE=true \
jefriherditriyanto/langchain-mcp-api:latest禁用详细模式
# Disable verbose logging (hides requestID-prefixed logs)
export VERBOSE=false
go run main.go
# Or simply don't set the variable (defaults to false)
go run main.go详细输出示例
当 VERBOSE=true,您将看到详细的执行痕迹:
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] [START REQUEST]
[1kqlh2PxHZZvLVLiBbdbWfdXQ9]📦 [AGENT] Creating LangChain Agent...
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] Provider: llama_cpp
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] Model: gpt-oss-20b.gguf
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] ✅ Loaded 22 tools from MCP servers
[1kqlh2PxHZZvLVLiBbdbWfdXQ9]🚀 [INVOKE] Starting agent invocation...
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] Input: What is the weather?
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] 🔁 [ITERATION 1/10]
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] 📝 Built 2 messages for LLM
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] 🤖 Calling LLM...
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] ✅ LLM Response (245 chars)
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] 🔧 Detected 1 tool call(s)
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] 1. getWeather({"lat": -7.7, "lon": 109.0})
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] ⚙️ Executing tools...
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] [1/1] Executing: getWeather
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] ✅ Success from http://host.docker.internal:4000
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] ✅ Tools executed successfully (1 results)
[1kqlh2PxHZZvLVLiBbdbWfdXQ9] ✅ [INVOKE] Agent invocation completed______________________________________________________________________
🐛 故障排除
Error: "No MCP servers available"
- 确保MCP服务器正在运行
- 检查服务器URL是否正确
- 验证健康端点:
curl http://localhost:4000/health
Error: "Missing api key"
- 验证请求中是否设置了API密钥
- 检查API密钥格式是否正确
- 确保提供程序名称匹配
Error: "Context size exceeded"
- 减少
max_context_messages(默认值:4) - 使用较短的系统提示
- 启用响应截断
Streaming not working
- 确保客户端支持服务器发送的事件
- 检查网络/代理设置
- 使用
-N带有卷曲的旗帜,用于流媒体播放
______________________________________________________________________
📊 演出
| 度量 | 值 |
|---|---|
| 启动时间 | \<1s |
| 内存使用 | ~50MB(空闲) |
| 并发请求 | 1000+ |
| 响应时间 | \<100ms(无LLM) |
______________________________________________________________________
📄 许可证
MIT许可证-请参阅 许可证 详细信息文件
______________________________________________________________________
👥 贡献者
杰弗里·赫迪·特里扬托 (@jefripunza)
______________________________________________________________________
🔗 相关项目
- MCP服务器 -模型上下文协议服务器实现
- LangChain Go -官方LangChain Go库
______________________________________________________________________
⭐ 如果你觉得这个仓库有用,就把它标上!
