checkConnectivity
Test TCP connectivity to a specified host and port, with configurable timeout, to diagnose network reachability and troubleshoot connection issues.
Instructions
Test TCP connectivity to a host and port
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| host | Yes | Target host | |
| port | Yes | Target port | |
| timeout | No | Connection timeout in milliseconds |
Implementation Reference
- src/tools/network.ts:50-82 (handler)The async handler function that tests TCP connectivity to the specified host and port using node:net.createConnection. Handles connection events, timeout, errors, and resolves with a JSON stringified NetworkConnectivityResult.handler: async ({ host, port, timeout = 5000 }: { host: string; port: number; timeout?: number }) => { return new Promise<{ content: Array<{ type: string, text: string }> }>((resolve) => { const result: NetworkConnectivityResult = { connected: false }; const socket = createConnection(port, host); socket.setTimeout(timeout); socket.on('connect', () => { result.connected = true; socket.end(); }); socket.on('timeout', () => { result.error = 'Connection timed out'; socket.destroy(); }); socket.on('error', (err) => { result.error = err.message; }); socket.on('close', () => { resolve({ content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }); }); }); }
- src/tools/network.ts:31-49 (schema)JSON schema for tool inputs: required host (string) and port (number), optional timeout (number, default 5000 ms).inputSchema: { type: 'object', properties: { host: { type: 'string', description: 'Target host' }, port: { type: 'number', description: 'Target port' }, timeout: { type: 'number', description: 'Connection timeout in milliseconds', default: 5000 } }, required: ['host', 'port'] },
- src/tools/network.ts:28-83 (registration)Tool definition and registration as part of the exported networkTools object, which is later spread into allTools in src/index.ts for MCP server handling.checkConnectivity: { name: 'checkConnectivity', description: 'Test TCP connectivity to a host and port', inputSchema: { type: 'object', properties: { host: { type: 'string', description: 'Target host' }, port: { type: 'number', description: 'Target port' }, timeout: { type: 'number', description: 'Connection timeout in milliseconds', default: 5000 } }, required: ['host', 'port'] }, handler: async ({ host, port, timeout = 5000 }: { host: string; port: number; timeout?: number }) => { return new Promise<{ content: Array<{ type: string, text: string }> }>((resolve) => { const result: NetworkConnectivityResult = { connected: false }; const socket = createConnection(port, host); socket.setTimeout(timeout); socket.on('connect', () => { result.connected = true; socket.end(); }); socket.on('timeout', () => { result.error = 'Connection timed out'; socket.destroy(); }); socket.on('error', (err) => { result.error = err.message; }); socket.on('close', () => { resolve({ content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }); }); }); } },