- Yeevin's Newsletter
- Posts
- 从0到1构建一个MCP Server
从0到1构建一个MCP Server
使用 Cline 和 MCP Inspector
MCP 解决了什么问题
LLM 做的事情本质上是在预测下一个 token,其生成的内容都是基于训练的结果,且有一定的随机性。如果能让 LLM 直接调用现成的API,就能保证结果的时效性和可靠性。MCP就是为了解决这个问题而提出的一种协议标准。
MCP 协议
MCP 是一个开放协议,它提供了一种标准的方法来连接 AI 模型和不同的数据源或工具。你可以把 MCP 想象成 AI 应用程序的 USB-C 接口,提供了一种标准化的方式将你的设备连接到各种外围设备和配件。
MCP 架构
类似 Client - Server 模式,MCP Host 通过 MCP Client 和 MCP Server 建立连接,一个MCP Host 可以连接多个 MCP Server。
MCP Host:需要使用到 MCP 的主体,比如 Claude客户端应用,IDE或者其他AI 工具。
MCP Client:MCP通过MCP Client 和 MCP Server 创建连接。
MCP Server:基于MCP 协议提供各种能力的服务。
使用 Cline 构建 MCP Server
安装 Cline
Cline 是一个 VSCode 插件,可以实现类似 Cursor的Coding Agent。配置Cline时我们需要提供 LLM 的 API Key,这里推荐使用 OpenRouter,其集成了多家模型服务,同时提供了一些免费的模型调用,包括 DeepSeek-R1。
注册 OpenRouter账号后,点击头像菜单,创建API Key。

把生成的 API Key 粘贴到 Cline 的配置中。

构建 MCP Server
这里我们没有采取 MCP 官网的示例手写MCP Server(插件),而是通过 Cline 自动构建。以下是基本的处理流程:
在 Cline 的 MCP 根目录下创建
.clinerules
文件,用于设置提示词引导模型如何识别和调用 MCP Server;(仅第一次需要设置)在 Cline 的聊天窗口中,描述清楚插件的用途、使用了什么API或资源
Cline 会自动切换到 Plan 模式,分析需要做哪些事情
如果确认模型输出的计划没有问题,则可以手动切换到 Act 模式开始自动执行
下面我们以生成一个实现时间戳和日期换算的MCP 插件,来演示如何操作。
生成 .clinerules
在Cline MCP的根目录下(/Users/your-name/Documents/Cline/MCP),创建 .clinerules
文件,并设置内容如下:
# MCP Plugin Development Protocol
⚠️ CRITICAL: DO NOT USE attempt_completion BEFORE TESTING ⚠️
## Step 1: Planning (PLAN MODE)
- What problem does this tool solve?
- What API/service will it use?
- What are the authentication requirements?
□ Standard API key
□ OAuth (requires separate setup script)
□ Other credentials
## Step 2: Implementation (ACT MODE)
1. Bootstrap
- For web services, JavaScript integration, or Node.js environments:
```bash
npx @modelcontextprotocol/create-server my-server
cd my-server
npm install
```
- For data science, ML workflows, or Python environments:
```bash
pip install mcp
# Or with uv (recommended)
uv add "mcp[cli]"
```
2. Core Implementation
- Use MCP SDK
- Implement comprehensive logging
- TypeScript (for web/JS projects):
```typescript
console.error('[Setup] Initializing server...');
console.error('[API] Request to endpoint:', endpoint);
console.error('[Error] Failed with:', error);
```
- Python (for data science/ML projects):
```python
import logging
logging.error('[Setup] Initializing server...')
logging.error(f'[API] Request to endpoint: {endpoint}')
logging.error(f'[Error] Failed with: {str(error)}')
```
- Add type definitions
- Handle errors with context
- Implement rate limiting if needed
3. Configuration
- Get credentials from user if needed
- Add to MCP settings:
- For TypeScript projects:
```json
{
"mcpServers": {
"my-server": {
"command": "node",
"args": ["path/to/build/index.js"],
"env": {
"API_KEY": "key"
},
"disabled": false,
"autoApprove": []
}
}
}
```
- For Python projects:
```bash
# Directly with command line
mcp install server.py -v API_KEY=key
# Or in settings.json
{
"mcpServers": {
"my-server": {
"command": "python",
"args": ["server.py"],
"env": {
"API_KEY": "key"
},
"disabled": false,
"autoApprove": []
}
}
}
```
## Step 3: Testing (BLOCKER ⛔️)
<thinking>
BEFORE using attempt_completion, I MUST verify:
□ Have I tested EVERY tool?
□ Have I confirmed success from the user for each test?
□ Have I documented the test results?
If ANY answer is "no", I MUST NOT use attempt_completion.
</thinking>
1. Test Each Tool (REQUIRED)
□ Test each tool with valid inputs
□ Verify output format is correct
⚠️ DO NOT PROCEED UNTIL ALL TOOLS TESTED
## Step 4: Completion
❗ STOP AND VERIFY:
□ Every tool has been tested with valid inputs
□ Output format is correct for each tool
Only after ALL tools have been tested can attempt_completion be used.
## Key Requirements
- ✓ Must use MCP SDK
- ✓ Must have comprehensive logging
- ✓ Must test each tool individually
- ✓ Must handle errors gracefully
- ⛔️ NEVER skip testing before completion
在聊天窗口中描述意图
我准备构建一个MCP插件用于时间戳的转换。它包括两个API,一个是把时间戳转换成北京时间的日期格式:yyyyMMdd HH:mm:ss,一个是北京时间的日期转换成对应的时间戳。插件使用typescript编写
在这之后,模型会自动指定执行计划,如果计划没有问题,则可以切换右下角的 Act 进行自动执行模型。在这过程中,模型会自动生成插件的代码,并且在每一个执行操作请会请求确认,一般只需要一直确认就可以。

最后模型会在 /Users/your-name/Documents/Cline/MCP 下生成对应的插件目录。点击右上角的 application 图表,可以看到已经识别并且安装好插件。

验证调用
输入 1741510520转日期

使用 MCP Inspector 进行调试
除了在聊天窗口调用,我们也可以通过 MCP Inspector 启动服务进行调试验证。通过以下脚本启动服务后,打开 http://localhost:5173 进行访问
npx @modelcontextprotocol/inspector node path/to/server/index.js
