Files
mcp-windows/src/tools/netstat.ts
T
Jonathan Miller 032c50aa46
Generic: Repo Health / Release configuration (push) Has been cancelled
Generic: Repo Health / Scripts governance (push) Has been cancelled
Generic: Repo Health / Repository health (push) Has been cancelled
Generic: Repo Health / Access control (push) Has been cancelled
Universal: Changelog Validation / Validate CHANGELOG.md (pull_request) Has been cancelled
MCP: Copilot Agent / Run Copilot Coding Agent (pull_request) Has been cancelled
Universal: Secret Scanning / Gitleaks Secret Scan (pull_request) Has been cancelled
Universal: PR Check / Branch Policy (pull_request) Has been cancelled
Universal: PR Check / Validate PR (pull_request) Has been cancelled
Generic: Repo Health / Access control (pull_request) Has been cancelled
Universal: PR Check / Changelog Updated (pull_request) Has been cancelled
MCP: Build & Validate / build (20) (pull_request) Has been cancelled
MCP: Build & Validate / build (22) (pull_request) Has been cancelled
MCP: Standards Compliance / Secret Scanning (pull_request) Has been cancelled
MCP: Standards Compliance / License Header Validation (pull_request) Has been cancelled
MCP: Standards Compliance / Repository Structure Validation (pull_request) Has been cancelled
MCP: Standards Compliance / Coding Standards Check (pull_request) Has been cancelled
MCP: Standards Compliance / Workflow Configuration Check (pull_request) Has been cancelled
MCP: Standards Compliance / Documentation Quality Check (pull_request) Has been cancelled
MCP: Standards Compliance / README Completeness Check (pull_request) Has been cancelled
MCP: Standards Compliance / Git Repository Hygiene (pull_request) Has been cancelled
MCP: Standards Compliance / File Naming Standards (pull_request) Has been cancelled
MCP: Standards Compliance / Insecure Code Pattern Detection (pull_request) Has been cancelled
MCP: Standards Compliance / Line Length Check (pull_request) Has been cancelled
MCP: Standards Compliance / Script Integrity Validation (pull_request) Has been cancelled
MCP: Standards Compliance / File Size Limits (pull_request) Has been cancelled
MCP: Standards Compliance / Dead Code Detection (pull_request) Has been cancelled
MCP: Standards Compliance / Binary File Detection (pull_request) Has been cancelled
MCP: Standards Compliance / TODO/FIXME Tracking (pull_request) Has been cancelled
MCP: Standards Compliance / Broken Link Detection (pull_request) Has been cancelled
MCP: Standards Compliance / API Documentation Coverage (pull_request) Has been cancelled
MCP: Standards Compliance / Accessibility Check (pull_request) Has been cancelled
MCP: Standards Compliance / Performance Metrics (pull_request) Has been cancelled
Universal: Auto-Assign / Assign unassigned issues and PRs (pull_request_target) Has been cancelled
MCP: Standards Compliance / Version Consistency Check (pull_request) Has been cancelled
MCP: Standards Compliance / Code Complexity Analysis (pull_request) Has been cancelled
MCP: Standards Compliance / Code Duplication Detection (pull_request) Has been cancelled
Universal: CodeQL Analysis / Analyze (actions) (pull_request) Has been cancelled
MCP: Standards Compliance / Terraform Configuration Validation (pull_request) Has been cancelled
Universal: CodeQL Analysis / Analyze (javascript) (pull_request) Has been cancelled
MCP: Standards Compliance / Unused Dependencies Check (pull_request) Has been cancelled
MCP: Standards Compliance / Enterprise Readiness Check (pull_request) Has been cancelled
MCP: Standards Compliance / Repository Health Check (pull_request) Has been cancelled
Universal: CodeQL Analysis / Security Scan Summary (pull_request) Has been cancelled
MCP: Standards Compliance / Dependency Vulnerability Scanning (pull_request) Has been cancelled
MCP: Standards Compliance / Compliance Summary (pull_request) Has been cancelled
Universal: Build & Release / Build & Release Pipeline (pull_request) Has been cancelled
Generic: Repo Health / Release configuration (pull_request) Has been cancelled
Generic: Repo Health / Scripts governance (pull_request) Has been cancelled
Generic: Repo Health / Repository health (pull_request) Has been cancelled
feat: implement v1.3 admin tools + v1.4 advanced (13 new tools)
v1.3 — Admin Tools:
- tools/scheduler.ts: windows_task_scheduler_list, _manage (#27, #28)
- tools/registry.ts: windows_registry_read, _write (#29, #30)
- tools/environment.ts: windows_env_get, _set (#31, #32)
- tools/startup.ts: windows_startup_list, _manage (#33, #34)
- tools/config.ts: windows_mcp_config (#40)

v1.4 — Advanced:
- tools/apps.ts: windows_installed_apps (#19)
- tools/dialog.ts: windows_dialog (#21)
- tools/netstat.ts: windows_network_connections (#23)
- tools/recycle_bin.ts: windows_recycle_bin (#26)

All 40 issues implemented. 44 tools total (some issues split into
sub-tools like terminal_start/send/read/list/kill).

Authored-by: Moko Consulting
2026-05-25 21:39:05 -05:00

73 lines
3.0 KiB
TypeScript

/* Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Tool: windows_network_connections (#23)
*/
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import { runPowerShell } from '../shell.js';
export function registerNetstatTools(server: McpServer): void {
server.tool(
'windows_network_connections',
'List active TCP/UDP network connections (like netstat). Shows local/remote address, state, and owning process.',
{
state: z.enum(['all', 'listen', 'established', 'time_wait', 'close_wait']).default('all').describe('Filter by state'),
port: z.number().optional().describe('Filter by port number'),
process_name: z.string().optional().describe('Filter by process name'),
limit: z.number().default(50).describe('Max results'),
},
async ({ state, port, process_name, limit }) => {
const stateMap: Record<string, string> = {
listen: "| Where-Object { $_.State -eq 'Listen' }",
established: "| Where-Object { $_.State -eq 'Established' }",
time_wait: "| Where-Object { $_.State -eq 'TimeWait' }",
close_wait: "| Where-Object { $_.State -eq 'CloseWait' }",
all: '',
};
const portFilter = port
? `| Where-Object { $_.LocalPort -eq ${port} -or $_.RemotePort -eq ${port} }`
: '';
const procFilter = process_name
? `| Where-Object { $procName -like '*${process_name.replace(/'/g, "''")}*' }`
: '';
const ps = `
Get-NetTCPConnection -ErrorAction SilentlyContinue ${stateMap[state]} ${portFilter} | ForEach-Object {
$proc = Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue
$procName = if ($proc) { $proc.ProcessName } else { '?' }
[PSCustomObject]@{
Proto = 'TCP'
LocalAddr = "$($_.LocalAddress):$($_.LocalPort)"
RemoteAddr = "$($_.RemoteAddress):$($_.RemotePort)"
State = $_.State.ToString()
PID = $_.OwningProcess
Process = $procName
}
} ${procFilter} | Select-Object -First ${limit} | ConvertTo-Json -Depth 3 -Compress`;
const result = await runPowerShell(ps, { timeout: 15000 });
if (result.exitCode !== 0) {
return { content: [{ type: 'text', text: `Error: ${result.stderr}` }], isError: true };
}
if (!result.stdout) {
return { content: [{ type: 'text', text: 'No connections found.' }] };
}
const conns = Array.isArray(JSON.parse(result.stdout)) ? JSON.parse(result.stdout) : [JSON.parse(result.stdout)];
const lines = conns.map((c: { Proto: string; LocalAddr: string; RemoteAddr: string; State: string; PID: number; Process: string }) =>
`${c.Proto} ${c.LocalAddr.padEnd(22)} ${c.RemoteAddr.padEnd(22)} ${c.State.padEnd(12)} ${String(c.PID).padStart(6)} ${c.Process}`,
);
const header = `Proto ${'Local Address'.padEnd(22)} ${'Remote Address'.padEnd(22)} ${'State'.padEnd(12)} ${'PID'.padStart(6)} Process`;
return {
content: [{ type: 'text', text: `${header}\n${'─'.repeat(100)}\n${lines.join('\n')}\n\n${conns.length} connections` }],
};
},
);
}