使用说明
数据工程导师MCP服务器
此仓库包含一个使用Node.js和TypeScript构建的简单模型上下文协议(MCP)服务器。它充当“数据工程导师”,为连接的AI客户端提供有关数据工程概念、模式和技术的个性化更新。
此服务器演示了MCP的关键概念:定义 资源, 工具,以及 鼓励 创建一个有状态的交互式代理助手。
先决条件
- Node.js(建议使用v18或更高版本)
npm(或者你喜欢的Node.js包管理器,比如yarn或pnpm)- 能够连接到MCP服务器的AI客户端(例如Cursor、Claude桌面应用程序)
- 一 OpenRouter API密钥 (用于通过困惑获取实时数据工程更新)
设置
-
克隆存储库:
# If you haven't already # git clone <repository-url> # cd <repository-directory> -
安装依赖关系:
npm install -
准备API密钥: 这
de_tutor_get_updates工具需要一个OpenRouter API密钥。- 从以下位置获取密钥 OpenRouter 的.
- 创建一个
.env项目根目录中的文件(您可以复制.env.example). - 将您的密钥添加到
.env文件:(将占位符替换为您的实际密钥。)OPENROUTER_API_KEY=sk-or-xxxxxxxxxxxxxxxxxxxxxxxxxx
-
构建服务器: 编译TypeScript代码。
npm run build
运行服务器
您可以直接使用Node运行服务器:
node build/index.js
或者,配置您的MCP客户端(如Cursor或Claude桌面应用程序)以启动服务器。服务器名称为 de-tutor 二进制名称(如果客户端配置需要)也是 de-tutor.
客户端配置示例(例如,用于Claude Desktop):
{
"mcpServers": {
"de-tutor": {
"command": "node",
"args": ["/full/path/to/your/project/build/index.js"],
"env": {
"OPENROUTER_API_KEY": "sk-or-xxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
}
(确保路径 args 是正确的吗 绝对路径 到建成 index.js 您系统上的文件。你可能不需要 env 如果您已经在使用 .env 文件,因为服务器直接通过以下方式加载它 dotenv.)
与光标一起使用
光标 是一个AI优先的代码编辑器,可以充当MCP客户端。使用Cursor设置此服务器需要配置服务器启动,并可能为指导提示设置项目规则,尽管Cursor也可能会选择服务器提供的提示。
-
在游标中配置服务器:
- 首选
Cursor Settings>MCP>Add new global MCP server. - 粘贴与上述示例客户端配置相同的JSON,确保路径
build/index.js适用于您的系统。
- 首选
-
(可选)为提示创建游标项目规则: 如果你更喜欢明确的规则,或者发现Cursor没有自动使用服务器的提示,你可以使用Cursor的 项目规则 功能。
-
创建目录
.cursor/rules如果它不存在,请在项目根目录中。 -
在其中创建一个名为的文件
de-tutor.rule(或任何.rule文件名)。 -
将以下指导文本粘贴到
de-tutor.rule:You are a helpful assistant connecting to a Data Engineering knowledge server. Your goal is to provide the user with personalized updates about new Data Engineering concepts, patterns, and technologies they haven't encountered yet. Available Tools: 1. `de_tutor_get_updates`: Fetches recent general news and articles about Data Engineering. Use this first to see what's new. 2. `de_tutor_read_memory`: Checks which Data Engineering concepts the user already knows based on their stored knowledge profile. 3. `de_tutor_write_memory`: Updates the user's profile to mark whether they have learned or already know a specific Data Engineering concept mentioned in an update. Your Workflow: 1. Call `de_tutor_get_updates` to discover recent Data Engineering developments. 2. Call `de_tutor_read_memory` to understand the user's current knowledge base. 3. Present the new developments to the user, highlighting things they likely don't know. 4. If the user confirms they know a concept or have learned it, call `de_tutor_write_memory` to update their profile. Be concise and focus on delivering relevant, new information tailored to the user's existing knowledge.
-
-
连接和使用:
- 确保
de-tutor服务器已在Cursor的MCP设置中启用。 - 如果使用规则文件:启动新的聊天或代码生成请求(例如Cmd+K),并包括
@de-tutor-rule(或您命名的规则文件)。这个命令告诉Cursor加载规则的内容,并提供如何使用工具的说明。 - 如果依赖服务器提示:只需开始与Cursor交互;它应该可以访问服务器提供的工具和指导提示。
- 确保
功能和用途
此服务器提供以下功能:
- 资源(
data_engineering_knowledge_memory): 在中存储一个简单的JSON对象data/data-engineering-knowledge.json将已知概念(字符串)映射到布尔标志(true). - 工具:
de_tutor_read_memory:从JSON文件中读取当前已知的概念。de_tutor_write_memory:更新JSON文件以将概念标记为已知(true)或未知(false).拿concept(字符串)和known(boolean)作为输入。de_tutor_get_updates:使用OpenRouter API密钥查询困惑(perplexity/sonar-small-online)了解最新的数据工程新闻、模式和技术。
- 提示(
data-engineering-tutor-guidance): 向连接的AI客户端提供有关如何在工作流中使用工具的说明:- 获取最新更新。
- 从记忆中读取已知概念。
- 向用户呈现新信息。
- 根据用户反馈更新内存。
开发与调试
- 构建:
npm run build将TypeScript编译为JavaScriptbuild/目录。 - 代码结构: 看
src/有关实施细节:src/index.ts:服务器入口点。进口McpServer和StdioServerTransport从特定的SDK路径。实例化McpServer.导入和调用注册功能(registerPrompts,registerResources,registerTools)从其他模块传递服务器实例。使用设置和连接服务器StdioServerTransport.src/prompts/index.ts:定义指导提示文本。出口registerPrompts,这需要McpServer实例和用途server.prompt()将静态引导提示注册到其回调中。src/resources/index.ts:出口KnowledgeMemory类型和辅助函数(readMemoryFile,writeMemoryFile)用于文件I/Odata/data-engineering-knowledge.json.出口registerResources,这需要McpServer实例和用途server.resource()注册data_engineering_knowledge_memory具有特定URI和ReadResourceCallback.src/tools/index.ts:出口registerTools,这需要McpServer实例和用途server.tool()注册每个工具(de_tutor_read_memory,de_tutor_write_memory,de_tutor_get_updates).必要时使用Zod定义输入模式(用于write_memory).工具函数使用来自的助手resources/index.ts或fetch执行操作并以预期格式返回结果。
- MCP检查员: 使用
@modelcontextprotocol/inspector查看原始消息流:(确保npx @modelcontextprotocol/inspector node ./build/index.jsOPENROUTER_API_KEY如果以这种方式运行并且不完全依赖于.env服务器本身加载的文件。)
备注
- 此服务器使用一个简单的文件(
data/data-engineering-knowledge.json)用于存储用户知识。对于更健壮的应用程序,请考虑使用合适的数据库。 - 错误处理是基本的;生产服务器将需要更全面的错误管理。
总结
此演示演示了使用TypeScript SDK和 McpServer 类。我们定义了管理状态的资源、执行操作的工具(包括与外部API交互),以及指导AI客户端的提示。
这为使用MCP构建更复杂、更有用的代理功能奠定了基础。
(另外,如果你遇到任何🐛bug,请随时提出问题。)