ip_validate
Validate IPv4 or IPv6 addresses to determine type, class, scope, and identify private, loopback, or multicast addresses for network configuration and security checks.
Instructions
Validate and classify an IPv4 or IPv6 address. Returns type, class, scope, and whether it's private/loopback/multicast.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ip | Yes | IP address to validate |
Implementation Reference
- src/tools/network.ts:113-209 (handler)The ip_validate tool handler, which validates IPv4 and IPv6 addresses and returns their classification details.
server.tool( "ip_validate", "Validate and classify an IPv4 or IPv6 address. Returns type, class, scope, and whether it's private/loopback/multicast.", { ip: z.string().describe("IP address to validate") }, async ({ ip }) => { // IPv4 check const ipv4Regex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/; const v4Match = ip.match(ipv4Regex); if (v4Match) { const parts = [ parseInt(v4Match[1]), parseInt(v4Match[2]), parseInt(v4Match[3]), parseInt(v4Match[4]), ]; if (parts.some((p) => p > 255)) { return { content: [ { type: "text" as const, text: JSON.stringify( { valid: false, error: "Octet value exceeds 255" }, null, 2 ), }, ], }; } const result = { valid: true, version: "IPv4", address: ip, class: parts[0] < 128 ? "A" : parts[0] < 192 ? "B" : parts[0] < 224 ? "C" : parts[0] < 240 ? "D (Multicast)" : "E (Reserved)", is_private: parts[0] === 10 || (parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) || (parts[0] === 192 && parts[1] === 168), is_loopback: parts[0] === 127, is_link_local: parts[0] === 169 && parts[1] === 254, is_multicast: parts[0] >= 224 && parts[0] <= 239, is_broadcast: parts.every((p) => p === 255), }; return { content: [ { type: "text" as const, text: JSON.stringify(result, null, 2) }, ], }; } // IPv6 basic check const ipv6Regex = /^([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}$/i; if (ipv6Regex.test(ip) || ip === "::1" || ip === "::") { const result = { valid: true, version: "IPv6", address: ip, is_loopback: ip === "::1", is_unspecified: ip === "::", is_link_local: ip.toLowerCase().startsWith("fe80"), is_multicast: ip.toLowerCase().startsWith("ff"), }; return { content: [ { type: "text" as const, text: JSON.stringify(result, null, 2) }, ], }; } return { content: [ { type: "text" as const, text: JSON.stringify( { valid: false, error: "Not a valid IPv4 or IPv6 address" }, null, 2 ), }, ], }; }