🚀 管理樣板 Model Context Protocol (MCP) 服務器實現使用 TypeScript
本項目使用 TypeScript 實現了管理樣板 Model Context Protocol (MCP) 服務器,可幫助你快速搭建 MCP 服務,提供了創建服務、客戶端使用、命令行工具等功能。
🚀 快速開始
安裝依賴項
要安裝所有依賴項,請運行:
npm install
安裝此包
通過以下命令安裝此包:
npm install -y @aashari/boilerplate-mcp-server
📦 安裝指南
通過以下命令安裝此包:
npm install -y @aashari/boilerplate-mcp-server
💻 使用示例
基礎用法
創建新 MCP 服務
- 初始化新項目:
npx tsx init
- 在
src/index.ts
文件中添加以下內容:
import { createServer } from '@aashari/boilerplate-mcp-server';
const server = createServer();
server.getIpDetails.addHandler((context) => {
return {
status: 'success',
country: '中國',
countryCode: 'CN',
region: 'BJ',
regionName: '北京',
city: '北京',
zip: '100000',
lat: '39.9045',
lon: '116.4074',
timezone: 'Asia/Shanghai',
isp: '中國移動',
org: 'China Mobile',
as: 'AS9009 中國互聯網有限公司',
query: context.ip,
};
});
server.listen(3000);
- 運行服務:
npm run dev
使用 MCP 客戶端
import { createClient } from '@aashari/boilerplate-mcp-server';
const client = createClient('http://localhost:3000');
client.getIpDetails().then(console.log);
client.getIpDetails('8.8.8.8').then(console.log);
高級用法
命令行工具(CLI)
可以直接使用命令行工具:
npx -y @aashari/boilerplate-mcp-server --help
示例輸出:
使用:@aashari/boilerplate-mcp-server [選項] [命令]
一個使用 TypeScript 實現的模型上下文協議(MCP)服務器的基本樣板
選項:
-V, --version 輸出版本號
-h, --help 顯示命令的幫助
命令:
get-ip-details [ipAddress] 獲取特定 IP 地址或當前設備的詳細信息
help [command] 顯示命令的幫助
npx -y @aashari/boilerplate-mcp-server get-ip-details
示例輸出:
狀態: 成功
國家: 印度尼西亞
國家代碼: ID
地區: JK
地區名稱: 雅加達
城市: 雅加達
郵編: 11730
緯度: -6.2114
經度: 106.8446
時間區: Asia/Jakarta
Isp: Biznet Wifi
組織:
As: AS17451 BIZNET NETWORKS
查詢: 118.923.134.234
npx -y @aashari/boilerplate-mcp-server get-ip-details 8.8.8.8
📚 詳細文檔
配置
環境變量
可以通過設置以下環境變量來配置服務器:
PORT
: 設置服務監聽的端口(默認值:3000)
PORT=8080 npm run dev
開發者指南
定製 MCP 服務
你可以通過擴展 createServer
函數來自定義 MCP 服務。
例如,添加自定義處理程序:
import { createServer } from '@aashari/boilerplate-mcp-server';
const server = createServer();
server.getIpDetails.addHandler((context) => {
return {
status: 'success',
country: '中國',
countryCode: 'CN',
region: 'BJ',
regionName: '北京',
city: '北京',
zip: '100000',
lat: '39.9045',
lon: '116.4074',
timezone: 'Asia/Shanghai',
isp: '中國移動',
org: 'China Mobile',
as: 'AS9009 中國互聯網有限公司',
query: context.ip,
};
});
server.listen(3000);
項目信息
屬性 |
詳情 |
版本 |
1.0.0 |
許可證 |
MIT |
作者 |
aashari |
郵件 |
[email protected] |
其他資源
📄 許可證
MIT 許可證
版權所有 (c) 2023, aashari.
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.