关于 MCP TypeScript Starter
MCP TypeScript Starter 是用于构建 MCP 服务的 TypeScript 模板,面向学习和原型开发。它展示工具、资源、模板和提示词,并提供 stdio、HTTP 入口及 VS Code 和 DevContainer 配置。
主要功能
- 01工具、资源和提示词示例
- 02资源模板与结构化工具输出
- 03进度更新和动态加载工具
- 04stdio 和 HTTP 传输
- 05VS Code 与 DevContainer 配置
适用场景
- 01学习 TypeScript 中的 MCP 服务模式
- 02使用示例工具和资源构建服务原型
- 03通过 VS Code 或 MCP Inspector 测试服务
MCP 相关
这是 MCP 服务模板,通过 stdio 或 HTTP 向 MCP 客户端提供示例工具、资源、资源模板和提示词。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
还没有获取到工具列表
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
MCP TypeScript 入门模板
一个功能完备的 TypeScript Model Context Protocol(MCP)服务模板。此入门模板以简洁、可用于生产环境的代码演示了 MCP 的所有主要功能。
📚 文档
✨ 功能
| 类别 | 功能 | 说明 |
|---|---|---|
| 工具 | hello | 带注解的基础工具 |
get_weather | 带结构化输出架构的工具 | |
ask_llm | 调用 LLM 采样的工具 | |
long_task | 每 5 秒更新一次进度的工具 | |
load_bonus_tool | 动态加载新工具 | |
| 资源 | info://about | 静态信息资源 |
file://example.md | 基于文件的 Markdown 资源 | |
| 模板 | greeting://{name} | 个性化问候语 |
data://items/{id} | 按 ID 查找数据 | |
| 提示词 | greet | 多种风格的问候语 |
code_review | 聚焦特定方面的代码审查 |
🚀 快速开始
前置条件
- Node.js 20+
- npm 或 pnpm
安装
# Clone the repository
git clone https://github.com/SamMorrowDrums/mcp-typescript-starter.git
cd mcp-typescript-starter
# Install dependencies
npm install
# Build
npm run build
运行服务
stdio 传输(用于本地开发):
npm run start:stdio
HTTP 传输(用于远程/Web 部署):
npm run start:http
# Server runs on http://localhost:3000
🔧 VS Code 集成
此项目包含 VS Code 配置,可实现流畅开发:
- 在 VS Code 中打开项目
- MCP 配置位于
.vscode/mcp.json - 使用
Ctrl+Shift+B(Mac 上使用Cmd+Shift+B)进行构建 - 使用 VS Code 的 MCP 工具测试服务
使用 DevContainers
- 安装 Dev Containers 扩展
- 打开命令面板:选择“Dev Containers: Reopen in Container”
- 所有内容都已预先配置完毕,可直接使用!
📁 项目结构
.
├── src/
│ ├── tools.ts # Tool definitions (hello, get_weather, ask_llm, etc.)
│ ├── resources.ts # Resource and template definitions
│ ├── prompts.ts # Prompt definitions
│ ├── server.ts # Server orchestration (combines all modules)
│ ├── stdio.ts # stdio transport entrypoint
│ └── http.ts # HTTP transport entrypoint
├── .vscode/
│ ├── mcp.json # MCP server configuration
│ ├── tasks.json # Build/run tasks
│ └── extensions.json
├── .devcontainer/
│ └── devcontainer.json
├── package.json
├── tsconfig.json
├── .prettierrc # Prettier configuration
└── eslint.config.js
🛠️ 开发
# Development mode with live reload
npm run dev
# Build for production
npm run build
# Format code
npm run format
# Lint
npm run lint
# Clean build
npm run clean && npm run build
实时重新加载
npm run dev 命令使用 tsx watch,可在开发期间即时重新加载。
对任何 .ts 文件的更改都会自动重启服务。
🔍 MCP Inspector
MCP Inspector 是用于测试和调试 MCP 服务的重要开发工具。
运行 Inspector
npx @modelcontextprotocol/inspector -- npx tsx src/stdio.ts
Inspector 提供的功能
- 工具选项卡:列出所有已注册工具及其参数,并可调用这些工具
- 资源选项卡:浏览和读取资源及模板
- 提示词选项卡:查看并测试提示词模板
- 日志选项卡:查看客户端与服务之间的 JSON-RPC 消息
- 架构验证:验证工具输入/输出架构
调试提示
- 在连接 IDE/客户端之前启动 Inspector
- 使用“Logs”选项卡查看准确的请求/响应载荷
- 测试工具注解是否正确公开
- 确认
long_task的进度通知是否出现
📖 功能示例
带注解的工具
server.tool(
"hello",
{
title: "Say Hello",
description: "A friendly greeting tool",
annotations: { readOnlyHint: true },
},
{ name: z.string() },
async ({ name }) => ({
content: [{ type: "text", text: `Hello, ${name}!` }],
})
);
资源模板
server.resourceTemplate(
"greeting://{name}",
{ name: "Personalized Greeting", mimeType: "text/plain" },
async ({ name }) => ({
contents: [{
uri: `greeting://${name}`,
text: `Hello, ${name}!`,
}],
})
);
带进度更新的工具
server.tool(
"long_task",
{ title: "Long Task" },
{ taskName: z.string() },
async ({ taskName }, { sendProgress }) => {
for (let i = 0; i < 5; i++) {
await sendProgress({ progress: i / 5, total: 1.0 });
await sleep(1000);
}
return { content: [{ type: "text", text: "Done!" }] };
}
);
🔐 环境变量
将 .env.example 复制为 .env 并进行配置:
cp .env.example .env
| 变量 | 说明 | 默认值 |
|---|---|---|
PORT | HTTP 服务端口 | 3000 |
🤝 参与贡献
欢迎贡献!请确保你的更改与其他语言入门项目保持功能一致。
📄 许可证
MIT 许可证 - 详情请参阅 LICENSE。
评论
支持 Markdown
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。