🚀 TypeScript SDK 使用说明
本 TypeScript SDK 为开发者提供了便捷的开发工具,可用于构建基于 MCP 的服务器应用,支持多种请求处理和数据库集成等功能。
🚀 快速开始
以下是一个简单的示例,展示如何使用 SDK:
import { Server } from "@modelcontextprotocol/typescript-sdk";
import { StdioServerTransport } from "@modelcontextprotocol/typescript-sdk";
const server = new Server({
name: "example-server",
version: "1.0.0"
});
server.setRequestHandler("ping", () => {
return {
status: "pong"
};
});
const transport = new StdioServerTransport();
await server.connect(transport);
✨ 主要特性
- 支持通过 npm 进行安装,也可在其他包管理平台使用。
- 提供多种请求处理示例,包括基本请求和复杂的数据库集成请求。
- 支持低层次实现和自定义 TypeScript 类型定义。
📦 安装指南
要安装 MCP TypeScript SDK,请按照以下步骤操作:
通过 npm 安装
npm install @modelcontextprotocol/typescript-sdk
其他平台
如需在其他平台(如 Yarn、pnpm 等)安装,请参考对应的包管理工具文档。
💻 使用示例
基础用法
import { Server } from "@modelcontextprotocol/typescript-sdk";
import { StdioServerTransport } from "@modelcontextprotocol/typescript-sdk";
const server = new Server({
name: "example-server",
version: "1.0.0"
});
server.setRequestHandler("ping", () => {
return {
status: "pong"
};
});
const transport = new StdioServerTransport();
await server.connect(transport);
高级用法
复杂示例(数据库集成)
import { Server } from "@modelcontextprotocol/typescript-sdk";
import sqlite3 from "sqlite3";
import { promisify } from "util";
import { z } from "zod";
const server = new Server({
name: "SQLite Explorer",
version: "1.0.0"
});
const getDb = () => {
const db = new sqlite3.Database("database.db");
return {
all: promisify(db.all.bind(db)),
close: promisify(db.close.bind(db))
};
};
server.setRequestHandler("schema", async () => {
const db = getDb();
try {
const tables = await db.all(
"SELECT sql FROM sqlite_master WHERE type='table'"
);
return {
contents: [{
uri: "schema://main",
text: tables.map((t: {sql: string}) => t.sql).join("\n")
}]
};
} finally {
await db.close();
}
});
server.setRequestHandler("query", async ({ body }) => {
const sql = body.text;
const db = getDb();
try {
const results = await db.all(sql);
return {
content: [{
type: "text",
text: JSON.stringify(results, null, 2)
}]
};
} catch (err: unknown) {
const error = err as Error;
return {
content: [{
type: "text",
text: `Error: ${error.message}`
}],
status: "error"
};
} finally {
await db.close();
}
});
const transport = new StdioServerTransport();
await server.connect(transport);
低层次实现
import { Server } from "@modelcontextprotocol/typescript-sdk";
import { RawServerTransport } from "@modelcontextprotocol/typescript-sdk";
const server = new Server({
name: "example-server",
version: "1.0.0"
});
server.setRequestHandler("ping", () => {
return {
status: "pong"
};
});
const transport = new RawServerTransport();
await server.connect(transport);
TypeScript 类型定义
import { Server } from "@modelcontextprotocol/typescript-sdk";
import { z } from "zod";
type User = {
id: string;
name: string;
email: string;
};
const userSchema = z.object({
id: z.string().min(1),
name: z.string().min(1),
email: z.string().email()
});
const server = new Server({
name: "example-server",
version: "1.0.0"
});
server.setRequestHandler("user", async ({ body }) => {
const user = await body.validate(userSchema);
return {
user
};
});
📚 详细文档
如需了解更多信息,请参考以下链接:
📄 许可证
本项目遵循 MIT 许可证,具体内容如下:
MIT License
Copyright (c) 2023 Example Corporation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.