aiwen-mcp-server-geoip
Version:
MCP server for using the aiwen geo loc API
170 lines (169 loc) • 5.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_js_1 = require("@modelcontextprotocol/sdk/server/index.js");
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
const node_fetch_1 = __importDefault(require("node-fetch"));
function getApiKey() {
const apiKey = process.env.AIWEN_API_KEY;
if (!apiKey) {
console.error("AIWEN_API_KEY environment variable is not set");
process.exit(1);
}
return apiKey;
}
const AIWEN_API_KEY = getApiKey();
// Tool definitions
const IP_LOCATION_TOOL = {
name: "aiwen_ip_location",
description: "IP定位 根据IP地址获取IP位置(支持城市/区县/街道三种精度)、经纬度、所属机构、运营商等信息",
inputSchema: {
type: "object",
properties: {
ip: {
type: "string",
description: "IP地址 IPv4或IPv6",
},
accuracy: {
type: "string",
description: "定位精度: city(城市级)、district(区县级)、street(街道级) ",
default: "city"
}
},
required: ["ip"],
}
};
const USER_NETWORK_IP = {
name: "user_network_ip",
description: "获取当前网络IP地址 根据当前网络IP地址获取位置信息",
inputSchema: {
type: "object",
properties: {},
},
};
const MAPS_TOOLS = [
IP_LOCATION_TOOL,
USER_NETWORK_IP,
];
const API_URL = "https://api.ipplus360.com/ip/geo/v1";
// API handlers
async function handleIPLocation(ip, accuracy = "city") {
// Determine IP type
const isIPv6 = ip.includes(":");
// Map accuracy to API endpoint suffix for IPv4 and IPv6
const apiSuffixMap = {
ipv4: {
city: "city/",
district: "district/",
street: "street/psi/",
},
ipv6: {
city: "ipv6/",
district: "ipv6/district/",
street: "ipv6/street/biz/",
},
};
const ipType = isIPv6 ? "ipv6" : "ipv4";
const accuracyMap = apiSuffixMap[ipType];
const apiSuffix = accuracyMap[accuracy] || accuracyMap["city"];
const url = new URL(`${API_URL}/${apiSuffix}`);
url.searchParams.append("key", AIWEN_API_KEY);
url.searchParams.append("channel", "node_mcp");
url.searchParams.append("coordsys", "WGS84");
url.searchParams.append("ip", ip);
// For IPv6 street, add area param (see Python logic)
if (ipType === "ipv6" && accuracy === "street") {
url.searchParams.append("area", "single");
}
const response = await (0, node_fetch_1.default)(url.toString());
const data = await response.json();
if (data.code !== "Success") {
return {
content: [{
type: "text",
text: `IP address query failed: ${data.msg || data}`
}],
isError: true
};
}
return {
content: [{
type: "text",
text: JSON.stringify(data, null, 2)
}],
isError: false
};
}
async function handleNetworkIp() {
const url = new URL("https://www.ipuu.net/ipuu/user/getIP");
url.searchParams.append("channel", "node_mcp");
const response = await (0, node_fetch_1.default)(url.toString());
const data = await response.json();
if (data.code !== 200) {
return {
content: [{
type: "text",
text: `user network ip query failed: ${data.msg || data.code}`
}],
isError: true
};
}
const ip = data.data;
return await handleIPLocation(ip);
}
// Server setup
const server = new index_js_1.Server({
name: "mcp-server/aiwen_geoip",
version: "1.0.0",
}, {
capabilities: {
tools: {},
},
});
// Set up request handlers
server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => ({
tools: MAPS_TOOLS,
}));
server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
try {
switch (request.params.name) {
case "aiwen_ip_location": {
const { ip, accuracy } = request.params.arguments;
return await handleIPLocation(ip, accuracy || "city");
}
case "user_network_ip": {
return await handleNetworkIp();
}
default:
return {
content: [{
type: "text",
text: `Unknown tool: ${request.params.name}`
}],
isError: true
};
}
}
catch (error) {
return {
content: [{
type: "text",
text: `Error: ${error instanceof Error ? error.message : String(error)}`
}],
isError: true
};
}
});
async function runServer() {
const transport = new stdio_js_1.StdioServerTransport();
await server.connect(transport);
console.error("Awien geoip MCP Server running on stdio");
}
runServer().catch((error) => {
console.error("Fatal error running server:", error);
process.exit(1);
});