使用说明
@mcpdotdirect/模板mcp服务器
使用FastMCP快速开始构建自己的MCP(模型上下文协议)服务器的CLI工具
📋 用法
# with npx
npx @mcpdotdirect/create-mcp-server
# Or with npm
npm init @mcpdotdirect/mcp-server
🔭 包含什么
模板包括:
- 使用FastMCP使用stdio和HTTP传输选项进行基本服务器设置
- 定义MCP工具、资源和提示的结构
- TypeScript配置
- 开发脚本和配置
✨ 特性
- FastMCP:使用FastMCP框架构建,实现更简单
- 双重运输支持:通过stdio或HTTP运行MCP服务器
- TypeScript:完全支持TypeScript以确保类型安全
- 可扩展:易于添加自定义工具、资源和提示
🚀 入门指南
创建项目后:
-
使用首选的包管理器安装依赖项:
# Using npm npm install # Using yarn yarn # Using pnpm pnpm install # Using bun bun install -
启动服务器:
# Start the stdio server npm start # Or start the HTTP server npm run start:http -
对于自动重新加载的开发:
# Development mode with stdio npm run dev # Development mode with HTTP npm run dev:http
备注package.json中的默认脚本使用Bun作为运行时(例如。,
bun run src/index.ts).如果你更喜欢使用不同的包管理器或运行时,你可以在package.json文件中修改这些脚本,以使用Node.js或你选择的其他运行时。
📖 详细用法
运输方式
MCP服务器支持两种传输方式:
-
stdio运输 (命令行模式):
- 运行在您的 本地计算机
- 由Cursor自动管理
- 通过以下方式直接沟通
stdout - 仅限您在本地访问
- 个人发展和工具的理想选择
-
SSE 运输 (HTTP Web模式):
- 可以运行 本地或远程
- 由您管理和运营
- 沟通 通过网络
- 可以是 共享 跨机器
- 非常适合团队协作和共享工具
在本地运行服务器
stdio传输(CLI模式)
在CLI工具的stdio模式下启动服务器:
# Start the stdio server
npm start
# or with other package managers
yarn start
pnpm start
bun start
# Start the server in development mode with auto-reload
npm run dev
# or
yarn dev
pnpm dev
bun dev
HTTP传输(Web模式)
以HTTP模式启动web应用程序的服务器:
# Start the HTTP server
npm run start:http
# or
yarn start:http
pnpm start:http
bun start:http
# Start the HTTP server in development mode with auto-reload
npm run dev:http
# or
yarn dev:http
pnpm dev:http
bun dev:http
默认情况下,HTTP服务器在端口3001上运行。您可以通过设置PORT环境变量来更改此设置:
# Start the HTTP server on a custom port
PORT=8080 npm run start:http
连接到服务器
从游标连接
要从Cursor连接到MCP服务器,请执行以下操作:
- 打开光标并转到设置(左下角的齿轮图标)
- 点击左侧边栏中的“功能”
- 向下滚动到“MCP服务器”部分
- 点击“添加新的MCP服务器”
- 输入以下详细信息:
- 服务器名称:
my-mcp-server(或您喜欢的任何名称) - 对于stdio模式:
- 类型:
command - 命令:指向服务器可执行文件的路径,例如。,
npm start
- 类型:
- 对于 SSE 模式:
- 类型:
url - 网址:
http://localhost:3001/sse
- 类型:
- 服务器名称:
- 点击“保存”
使用mcp.json和游标
要获得更便携的配置,请创建 .cursor/mcp.json 项目根目录中的文件:
{
"mcpServers": {
"my-mcp-stdio": {
"command": "npm",
"args": [
"start"
],
"env": {
"NODE_ENV": "development"
}
},
"my-mcp-sse": {
"url": "http://localhost:3001/sse"
}
}
}
您还可以在以下位置创建全局配置 ~/.cursor/mcp.json 使您的MCP服务器在所有Cursor工作区中可用。
注:
- 这
command类型条目以stdio模式运行服务器 - 这
url类型条目使用SSE传输连接到HTTP服务器 - 您可以使用以下命令提供环境变量
env领域 - 当通过SSE与FastMCP连接时,请使用完整的URL,包括
/sse路径:http://localhost:3001/sse
使用CLI工具测试服务器
FastMCP提供用于测试服务器的内置工具:
# Test with mcp-cli
npx fastmcp dev server.js
# Inspect with MCP Inspector
npx fastmcp inspect server.ts
使用环境变量
您可以使用环境变量自定义服务器:
# Change the HTTP port (default is 3001)
PORT=8080 npm run start:http
# Change the host binding (default is 0.0.0.0)
HOST=127.0.0.1 npm run start:http
🛠️ 添加自定义工具和资源
向FastMCP服务器添加自定义工具、资源或提示时:
工具
server.addTool({
name: "hello_world",
description: "A simple hello world tool",
parameters: z.object({
name: z.string().describe("Name to greet")
}),
execute: async (params) => {
return `Hello, ${params.name}!`;
}
});
资源
server.addResourceTemplate({
uriTemplate: "example://{id}",
name: "Example Resource",
mimeType: "text/plain",
arguments: [
{
name: "id",
description: "Resource ID",
required: true,
},
],
async load({ id }) {
return {
text: `This is an example resource with ID: ${id}`
};
}
});
鼓励
server.addPrompt({
name: "greeting",
description: "A simple greeting prompt",
arguments: [
{
name: "name",
description: "Name to greet",
required: true,
},
],
load: async ({ name }) => {
return `Hello, ${name}! How can I help you today?`;
}
});
📚 文档
有关FastMCP的更多信息,请访问 FastMCP GitHub仓库.
有关模型上下文协议的更多信息,请访问 MCP文件.
📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
