MCP Model Provider Example
Demonstrates a complete MCP server implementation supporting both local Ollama models and remote API models. Enables LangChain applications to dynamically discover and invoke tools including addition calculations and model invocation capabilities.
README
MCP 模型提供商示例
这是一个完整的 MCP (Model Context Protocol) 示例项目,展示了如何搭建一个支持本地 Ollama 模型和远程 API 模型的 MCP 服务器,以及如何使用 LangChain 构建大模型应用来发现和调用 MCP 工具。
功能特性
- 支持本地模型:通过 Ollama 部署的本地模型,无需 API 密钥
- 支持远程模型:通过 API 密钥访问远程模型(如 OpenAI)
- 动态工具发现:大模型可以自动发现 MCP 提供的工具
- 工具调用:大模型可以根据需要调用 MCP 工具并获取结果
- 加法计算工具:内置了一个计算两个数之和的工具
- 模型调用工具:可以调用不同类型的语言模型
项目结构
e:\agents_pro\mcp\
├── package.json # 项目配置和依赖
├── index.js # MCP 服务器主文件
├── tools/ # 工具定义目录
│ ├── model_invoke.json # 模型调用工具定义
│ └── add_numbers.json # 加法计算工具定义
├── example.js # JavaScript 示例
├── langchain-app.py # Python LangChain 应用示例
└── README.md # 项目文档
安装步骤
1. 安装 Node.js 依赖
npm install
2. 安装 Python 依赖(用于 LangChain 应用)
pip install -r requirements.txt
3. 安装 Ollama(用于本地模型)
请访问 Ollama 官网 下载并安装 Ollama,然后运行以下命令拉取模型:
ollama pull llama2
ollama pull qwen3:4b
配置说明
1. MCP 服务器配置
- 端口:默认运行在 3000 端口
- 工具定义:在
tools/目录下定义工具 - 模型配置:
- 本地模型:通过 Ollama 部署,默认地址为
http://localhost:11434 - 远程模型:需要在代码中设置 API 密钥
- 本地模型:通过 Ollama 部署,默认地址为
2. LangChain 应用配置
- 模型选择:默认使用本地 Ollama 模型
qwen3:4b - 远程模型:需要在
langchain-app.py中设置 API 密钥
使用方法
1. 启动 MCP 服务器
npm start
服务器启动后,会在控制台输出以下信息:
MCP server running on port 3000
Tools available at: http://localhost:3000/mcp/tools
Invoke endpoint at: http://localhost:3000/mcp/invoke
2. 运行 JavaScript 示例
node example.js
3. 运行 Python LangChain 应用
python langchain-app.py
API 端点
1. 列出可用工具
请求:
GET /mcp/tools
响应:
{
"tools": [
{
"name": "model_invoke",
"description": "Invoke a language model with a prompt",
"parameters": {
"type": "object",
"properties": {
"prompt": {
"type": "string",
"description": "The prompt to send to the model"
},
"model_type": {
"type": "string",
"enum": ["local", "remote"],
"description": "Model type: local (Ollama) or remote (API)"
},
"model_name": {
"type": "string",
"description": "Model name to use"
},
"api_key": {
"type": "string",
"description": "API key for remote model (required if model_type is remote)"
}
},
"required": ["prompt", "model_type", "model_name"]
}
},
{
"name": "add_numbers",
"description": "Calculate the sum of two numbers",
"parameters": {
"type": "object",
"properties": {
"num1": {
"type": "number",
"description": "The first number"
},
"num2": {
"type": "number",
"description": "The second number"
}
},
"required": ["num1", "num2"]
}
}
]
}
2. 调用工具
请求:
POST /mcp/invoke
Content-Type: application/json
{
"tool": "model_invoke",
"params": {
"prompt": "你好,告诉我一个简短的笑话",
"model_type": "local",
"model_name": "llama2"
}
}
响应:
{
"success": true,
"data": {
"response": "为什么人工智能不会感冒?因为它们没有病毒,只有bug!"
}
}
示例场景
1. 大模型自动发现并使用加法工具
用户查询:"计算 123 加上 456 等于多少"
大模型流程:
- 分析用户请求,发现需要进行加法计算
- 自动发现 MCP 提供的
add_numbers工具 - 调用工具,传递参数
{"num1": 123, "num2": 456} - 接收工具执行结果
{"sum": 579} - 基于结果生成最终回答:"123 加上 456 等于 579"
2. 大模型使用模型调用工具
用户查询:"使用本地模型写一首关于春天的诗"
大模型流程:
- 分析用户请求,发现需要使用模型生成内容
- 自动发现 MCP 提供的
model_invoke工具 - 调用工具,传递参数
{"prompt": "写一首关于春天的诗", "model_type": "local", "model_name": "qwen3:4b"} - 接收工具执行结果(模型生成的诗)
- 基于结果生成最终回答
注意事项
- 本地模型:使用本地 Ollama 模型时,需要确保 Ollama 服务正在运行
- 远程模型:使用远程模型时,需要在代码中设置有效的 API 密钥
- 工具定义:添加新工具时,需要在
tools/目录下创建工具定义文件,并在index.js中添加相应的处理函数 - 端口冲突:如果端口 3000 被占用,可以修改
index.js中的端口配置
扩展建议
- 添加更多工具:可以根据需要添加更多工具,如天气查询、新闻获取等
- 支持更多模型:可以扩展
model_invoke工具,支持更多类型的模型提供商 - 添加认证:可以为 MCP 服务器添加认证机制,确保只有授权的应用可以访问
- 添加日志:可以添加详细的日志记录,便于调试和监控
故障排除
1. 端口被占用
如果启动服务器时出现端口被占用的错误,可以使用以下命令查看并终止占用端口的进程:
# 查看占用端口 3000 的进程
netstat -ano | findstr :3000
# 终止进程(PID 为上一步输出的进程 ID)
taskkill /PID <PID> /F
2. 本地模型不可用
如果本地模型不可用,请检查:
- Ollama 服务是否正在运行
- 模型是否已经拉取(使用
ollama list查看) - 模型名称是否正确
3. 远程模型调用失败
如果远程模型调用失败,请检查:
- API 密钥是否正确
- 网络连接是否正常
- 模型名称是否正确
总结
本项目展示了如何搭建一个完整的 MCP 服务器,以及如何使用 LangChain 构建大模型应用来发现和调用 MCP 工具。通过这个项目,您可以了解:
- MCP 的基本概念和工作原理
- 如何定义和实现 MCP 工具
- 如何使用 LangChain 构建大模型应用
- 大模型如何自动发现和调用工具
- 如何支持本地和远程模型
这个项目可以作为一个基础框架,根据实际需求进行扩展和定制。
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.