Cursor编辑器Model Context Protocol配置指南

Cursor编辑器Model Context Protocol配置指南

Cursor是一款强大的AI驱动编辑器,通过Model Context Protocol (MCP),它能够与各种AI模型和服务进行交互,扩展编辑器的功能。本文将详细介绍如何在Cursor中配置MCP,让你的编辑器更加智能和高效。

什么是Model Context Protocol (MCP)

Model Context Protocol (MCP) 是一个开放协议,允许你为Cursor中的AI代理提供自定义工具。通过MCP,你可以:

  • 扩展AI的能力,使其能够访问外部服务和工具
  • 提供特定领域的专业知识和功能
  • 实现更加智能和上下文感知的编码辅助

Cursor实现了MCP客户端,支持任意数量的MCP服务器。目前,Cursor的MCP客户端支持stdiosse两种传输方式。

MCP配置文件

在Cursor中配置MCP,需要创建一个名为mcp.json的配置文件,放置在.cursor目录下。配置文件的基本结构如下:

1
2
3
4
5
6
7
8
9
10
{
"servers": [
{
"name": "服务器名称",
"type": "传输类型",
"command": "启动命令",
"enabled": true
}
]
}

配置参数说明

  • name: MCP服务器的名称,用于在Cursor界面中显示
  • type: 传输类型,可以是stdiosse
    • stdio: 标准输入输出传输,适用于本地运行的服务器
    • sse: Server-Sent Events传输,适用于远程运行的服务器
  • command: 启动服务器的命令(对于stdio类型)或服务器URL(对于sse类型)
  • enabled: 是否启用该服务器

常用MCP服务器配置示例

以下是几个常用MCP服务器的配置示例:

Sequential Thinking

Sequential Thinking是一个帮助AI进行逐步思考的工具,可以提高AI解决复杂问题的能力。

1
2
3
4
5
6
{
"name": "Sequential Thinking",
"type": "stdio",
"command": "npx -y @modelcontextprotocol/server-sequential-thinking",
"enabled": true
}

Puppeteer

Puppeteer是一个浏览器自动化工具,可以让AI控制浏览器进行网页操作和信息获取。

1
2
3
4
5
6
{
"name": "Puppeteer",
"type": "stdio",
"command": "npx -y @modelcontextprotocol/server-puppeteer",
"enabled": true
}

Brave Search允许AI直接搜索网络获取信息,需要配置API密钥。

1
2
3
4
5
6
{
"name": "Brave Search",
"type": "stdio",
"command": "env BRAVE_API_KEY=your-api-key-here npx -y @modelcontextprotocol/server-brave-search",
"enabled": true
}

完整配置示例

以下是一个包含上述所有服务器的完整配置示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"servers": [
{
"name": "Brave Search",
"type": "stdio",
"command": "env BRAVE_API_KEY=your-api-key-here npx -y @modelcontextprotocol/server-brave-search",
"enabled": true
},
{
"name": "Sequential Thinking",
"type": "stdio",
"command": "npx -y @modelcontextprotocol/server-sequential-thinking",
"enabled": true
},
{
"name": "Puppeteer",
"type": "stdio",
"command": "npx -y @modelcontextprotocol/server-puppeteer",
"enabled": true
}
]
}

如何添加MCP配置

  1. 在Cursor工作目录下创建.cursor文件夹(如果不存在)
  2. .cursor文件夹中创建mcp.json文件
  3. 将上述配置示例复制到文件中,根据需要修改
  4. 重启Cursor编辑器使配置生效

也可以通过Cursor的设置界面添加MCP服务器:

  1. 打开Cursor设置
  2. 导航到”Features”部分
  3. 滚动到”MCP Servers”部分
  4. 点击”+ Add New MCP Server”按钮
  5. 填写服务器信息并保存

使用MCP工具

配置好MCP后,你可以在Cursor的Composer和Agent模式中使用这些工具。需要注意的是:

  1. MCP工具只在Composer和Agent模式下可用
  2. 需要明确指示AI使用MCP工具
  3. 当工具使用提示出现时,需要接受它们

故障排除

如果MCP服务器显示黄色状态,可以尝试以下方法:

  1. 刷新Cursor
  2. 确保Cursor版本是最新的
  3. 检查API密钥格式是否正确
  4. 如果工具没有出现,尝试重启Cursor

总结

通过配置Model Context Protocol,你可以大大扩展Cursor编辑器的AI能力,使其能够访问更多外部服务和工具。本文介绍了MCP的基本概念、配置方法和常用服务器示例,希望能帮助你更好地利用Cursor的AI功能,提高编码效率。

随着AI技术的不断发展,MCP将支持更多类型的服务器和工具,为开发者提供更加强大和灵活的AI辅助能力。

本文永久链接: https://www.mulianju.com/2024/cursor-model-context-protocol-configuration/