register-service
Register a service with Consul by specifying its name, ID, port, address, and optional tags. Simplifies service discovery and management within the Consul MCP Server framework.
Instructions
Register a service with Consul
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| address | No | Address the service is running on | |
| id | No | ID of the service (defaults to name if not provided) | |
| name | No | Name of the service to register | |
| port | No | Port the service is running on | |
| tags | No | Tags to associate with the service |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"address": {
"default": "",
"description": "Address the service is running on",
"type": "string"
},
"id": {
"default": "",
"description": "ID of the service (defaults to name if not provided)",
"type": "string"
},
"name": {
"default": "",
"description": "Name of the service to register",
"type": "string"
},
"port": {
"description": "Port the service is running on",
"type": "number"
},
"tags": {
"description": "Tags to associate with the service",
"items": {
"type": "string"
},
"type": "array"
}
},
"type": "object"
}
Implementation Reference
- src/tools/consulTools.ts:302-324 (handler)The handler function for the 'register-service' tool. It constructs a service definition from inputs and calls consul.agent.service.register to register the service with Consul.async ({ name, id, port, address, tags }) => { try { const serviceId = id || name; const serviceDefinition: any = { name, id: serviceId, }; if (port !== undefined) serviceDefinition.port = port; if (address !== undefined) serviceDefinition.address = address; if (tags !== undefined) serviceDefinition.tags = tags; await consul.agent.service.register(serviceDefinition); //if (!success) { // return { content: [{ type: "text", text: `Failed to register service: ${name}` }] }; //} return { content: [{ type: "text", text: `Successfully registered service: ${name} with ID: ${serviceId}` }] }; } catch (error) { console.error("Error registering service:", error); return { content: [{ type: "text", text: `Error registering service: ${name}` }] }; } }
- src/tools/consulTools.ts:295-301 (schema)Zod schema defining the input parameters for the 'register-service' tool.{ name: z.string().default("").describe("Name of the service to register"), id: z.string().default("").optional().describe("ID of the service (defaults to name if not provided)"), port: z.number().optional().describe("Port the service is running on"), address: z.string().default("").optional().describe("Address the service is running on"), tags: z.array(z.string()).optional().describe("Tags to associate with the service"), },
- src/tools/consulTools.ts:292-325 (registration)Registration of the 'register-service' tool on the MCP server using server.tool(), including description, input schema, and handler function.server.tool( "register-service", "Register a service with Consul", { name: z.string().default("").describe("Name of the service to register"), id: z.string().default("").optional().describe("ID of the service (defaults to name if not provided)"), port: z.number().optional().describe("Port the service is running on"), address: z.string().default("").optional().describe("Address the service is running on"), tags: z.array(z.string()).optional().describe("Tags to associate with the service"), }, async ({ name, id, port, address, tags }) => { try { const serviceId = id || name; const serviceDefinition: any = { name, id: serviceId, }; if (port !== undefined) serviceDefinition.port = port; if (address !== undefined) serviceDefinition.address = address; if (tags !== undefined) serviceDefinition.tags = tags; await consul.agent.service.register(serviceDefinition); //if (!success) { // return { content: [{ type: "text", text: `Failed to register service: ${name}` }] }; //} return { content: [{ type: "text", text: `Successfully registered service: ${name} with ID: ${serviceId}` }] }; } catch (error) { console.error("Error registering service:", error); return { content: [{ type: "text", text: `Error registering service: ${name}` }] }; } } );