MCP服务器TypeScript入门
这是一个提供位置服务的模型上下文协议(MCP)服务器实现。它演示了如何使用TypeScript构建一个MCP服务器,该服务器具有通过坐标或IP地址进行位置查找的真实功能。
特性
- TypeScript配置
- 完成MCP服务器设置
- 位置服务实施
- 基于坐标的位置查找 - 基于IP的位置查找(使用IPInfo.io) - 健康检查端点
- 类型安全开发环境
- 与外部API集成
安装
npm install mylocation-mcp用法
- 创建一个
.env文件与您的IPInfo.io API令牌:
IPINFO_TOKEN=your_ipinfo_token_here- 导入并在代码中使用:
import { McpServer } from 'mylocation-mcp';发展
如果您想修改或贡献包:
# Clone the repository
git clone https://github.com/yhwancha/mylocation-mcp.git
cd mylocation-mcp
# Install dependencies
npm install
# Set up environment variables
cp .env.example .env
# Edit .env and add your IPInfo.io API token
# Build the project
npm run build项目结构
.
├── src/
│ └── index.ts # Main server implementation with location tools
├── build/ # Compiled JavaScript files
├── package.json # Project dependencies and scripts
├── tsconfig.json # TypeScript configuration
├── .env.example # Environment variables template
└── README.md # Documentation已实施的工具
1.通过坐标获取位置
server.tool(
"get-location-by-coordinates",
"Get location information from provided coordinates",
{
latitude: z.string().describe("Latitude coordinate (-90 to 90)"),
longitude: z.string().describe("Longitude coordinate (-180 to 180)")
},
async ({ latitude, longitude }) => {
// Implementation details...
}
);2.通过ip获取位置
server.tool(
"get-location-by-ip",
"Get location information from IP address",
{
ipAddress: z.string().describe("IP address to lookup")
},
async ({ ipAddress }) => {
// Implementation details...
}
);3.健康
server.tool(
"health",
"Check the health status of the service",
{},
async () => {
// Implementation details...
}
);MCP服务器配置
要在项目中使用此MCP服务器,请添加以下配置:
{
"mcpServers": {
"location-service": {
"command": "npx",
"args": [
"-y",
"mylocation-mcp-server"
],
"env": {
"IPINFO_TOKEN":
}
}
}
}响应格式
所有工具都以以下格式返回响应:
{
"content": [
{
"type": "text",
"text": "JSON string containing the response data"
}
]
}响应数据遵循以下结构:
{
"status": "success|error",
"source": "user_provided|ip_based",
"data": {
// Location data when successful
},
"error": "Error message when failed"
}依赖项
@modelcontextprotocol/sdk:MCP服务器实现zod:架构验证axios:IPInfo API的HTTP客户端dotenv:环境变量管理- TypeScript开发工具
许可证
麻省理工学院
