管理样板 Model Context Protocol (MCP) 服务器实现使用 TypeScript
安装
通过以下命令安装此包:
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] 显示命令的帮助
-
获取当前设备的 IP 详细信息:
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
-
获取指定 IP 的详细信息:
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]
安装依赖项
要安装所有依赖项,请运行:
npm install
许可证
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.
命令行示例
安装包
npm install -y @aashari/boilerplate-mcp-server
运行服务
npx tsx init && npm run dev
使用客户端
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);
命令行工具
npx -y @aashari/boilerplate-mcp-server get-ip-details 8.8.8.8
其他资源
这个文档涵盖了如何安装、配置和使用 @aashari/boilerplate-mcp-server
包,包括命令行工具的使用和自定义服务器的实现。希望对你有所帮助!