🚀 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.