@aashari/boilerplate-mcp-server
Version:
TypeScript Model Context Protocol (MCP) server boilerplate providing IP lookup tools/resources. Includes CLI support and extensible structure for connecting AI systems (LLMs) to external data sources like ip-api.com. Ideal template for creating new MCP in
67 lines (66 loc) • 3.56 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const logger_util_js_1 = require("../utils/logger.util.js");
const ipaddress_types_js_1 = require("./ipaddress.types.js");
const error_util_js_1 = require("../utils/error.util.js");
const zod_1 = require("zod");
const ipaddress_controller_js_1 = __importDefault(require("../controllers/ipaddress.controller.js"));
/**
* Zod schema for the tool arguments, combining the optional positional IP address
* and the options object.
*/
const GetIpDetailsToolSchema = zod_1.z.object({
ipAddress: zod_1.z
.string()
.optional()
.describe('IP address to lookup (omit for current IP)'),
...ipaddress_types_js_1.IpAddressToolArgs.shape, // Merge options schema
});
/**
* @function handleGetIpDetails
* @description MCP Tool handler to retrieve details for a given IP address (or the current IP).
* It calls the ipAddressController to fetch the data and formats the response for the MCP.
*
* @param {GetIpDetailsToolArgsType} args - Combined arguments (ipAddress + options) provided to the tool.
* @returns {Promise<{ content: Array<{ type: 'text', text: string }> }>} Formatted response for the MCP.
* @throws {McpError} Formatted error if the controller or service layer encounters an issue.
*/
async function handleGetIpDetails(args) {
const methodLogger = logger_util_js_1.Logger.forContext('tools/ipaddress.tool.ts', 'handleGetIpDetails');
methodLogger.debug(`Getting IP address details for ${args.ipAddress || 'current IP'}...`, args);
try {
// Pass args directly to the controller
const result = await ipaddress_controller_js_1.default.get(args);
methodLogger.debug(`Got the response from the controller`, result);
// Format the response for the MCP tool
return {
content: [
{
type: 'text',
text: result.content,
},
],
};
}
catch (error) {
methodLogger.error(`Error getting details for IP: ${args.ipAddress || 'current IP'}`, error);
return (0, error_util_js_1.formatErrorForMcpTool)(error);
}
}
/**
* @function registerTools
* @description Registers the IP address lookup tool ('ip_get_details') with the MCP server.
*
* @param {McpServer} server - The MCP server instance.
*/
function registerTools(server) {
const methodLogger = logger_util_js_1.Logger.forContext('tools/ipaddress.tool.ts', 'registerTools');
methodLogger.debug(`Registering IP address tools...`);
server.tool('ip_get_details', `Retrieves detailed geolocation and network information for a public IP address (\`ipAddress\`). If no IP is provided, automatically uses the server's current public IP. Returns comprehensive location data including country, region, city, coordinates, timezone, and network details like ISP and organization. Use \`includeExtendedData\` to get additional information such as ASN, mobile/proxy detection (requires API token). **Note:** Cannot lookup private IPs (e.g., 192.168.x.x, 10.x.x.x). Powered by ip-api.com. Enable \`useHttps\` for secure connections (required for paid tier).`, GetIpDetailsToolSchema.shape, // Use the combined schema for validation
handleGetIpDetails);
methodLogger.debug('Successfully registered ip_get_details tool.');
}
exports.default = { registerTools };