-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
166 lines (140 loc) · 4.05 KB
/
main.js
File metadata and controls
166 lines (140 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env -S deno run --allow-run --allow-read --allow-env --allow-write
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2025 Jonathan D.A. Jewell
// poly-k8s-mcp - Kubernetes orchestration MCP server
// Adapters: kubectl, helm, kustomize
import * as Kubectl from "./lib/es6/src/adapters/Kubectl.res.js";
import * as Helm from "./lib/es6/src/adapters/Helm.res.js";
import * as Kustomize from "./lib/es6/src/adapters/Kustomize.res.js";
const SERVER_INFO = {
name: "poly-k8s-mcp",
version: "1.0.0",
description: "Kubernetes orchestration MCP server (kubectl, helm, kustomize)",
};
// Combine all tools from adapters
const allTools = {
...Kubectl.tools,
...Helm.tools,
...Kustomize.tools,
};
// Route tool calls to appropriate adapter
async function handleToolCall(name, args) {
if (name.startsWith("kubectl_")) {
return await Kubectl.handleToolCall(name, args);
} else if (name.startsWith("helm_")) {
return await Helm.handleToolCall(name, args);
} else if (name.startsWith("kustomize_")) {
return await Kustomize.handleToolCall(name, args);
}
return { TAG: "Error", _0: `Unknown tool: ${name}` };
}
// MCP Protocol handlers
function handleInitialize(id) {
return {
jsonrpc: "2.0",
id,
result: {
protocolVersion: "2024-11-05",
serverInfo: SERVER_INFO,
capabilities: {
tools: { listChanged: false },
},
},
};
}
function handleToolsList(id) {
const toolsList = Object.values(allTools).map((tool) => ({
name: tool.name,
description: tool.description,
inputSchema: tool.inputSchema,
}));
return {
jsonrpc: "2.0",
id,
result: { tools: toolsList },
};
}
async function handleToolsCall(id, params) {
const { name, arguments: args } = params;
const result = await handleToolCall(name, args || {});
if (result.TAG === "Ok") {
return {
jsonrpc: "2.0",
id,
result: {
content: [{ type: "text", text: result._0 }],
},
};
} else {
return {
jsonrpc: "2.0",
id,
result: {
content: [{ type: "text", text: `Error: ${result._0}` }],
isError: true,
},
};
}
}
// Main message handler
async function handleMessage(message) {
const { method, id, params } = message;
switch (method) {
case "initialize":
return handleInitialize(id);
case "initialized":
return null; // Notification, no response
case "tools/list":
return handleToolsList(id);
case "tools/call":
return await handleToolsCall(id, params);
default:
return {
jsonrpc: "2.0",
id,
error: { code: -32601, message: `Method not found: ${method}` },
};
}
}
// stdio transport
const decoder = new TextDecoder();
const encoder = new TextEncoder();
async function readMessage() {
const buffer = new Uint8Array(65536);
let data = "";
while (true) {
const n = await Deno.stdin.read(buffer);
if (n === null) return null;
data += decoder.decode(buffer.subarray(0, n));
// Look for Content-Length header and complete message
const headerEnd = data.indexOf("\r\n\r\n");
if (headerEnd === -1) continue;
const header = data.substring(0, headerEnd);
const contentLengthMatch = header.match(/Content-Length: (\d+)/i);
if (!contentLengthMatch) continue;
const contentLength = parseInt(contentLengthMatch[1]);
const bodyStart = headerEnd + 4;
const bodyEnd = bodyStart + contentLength;
if (data.length < bodyEnd) continue;
const body = data.substring(bodyStart, bodyEnd);
data = data.substring(bodyEnd);
return JSON.parse(body);
}
}
function writeMessage(message) {
const body = JSON.stringify(message);
const header = `Content-Length: ${encoder.encode(body).length}\r\n\r\n`;
Deno.stdout.writeSync(encoder.encode(header + body));
}
// Main loop
async function main() {
while (true) {
const message = await readMessage();
if (message === null) break;
const response = await handleMessage(message);
if (response !== null) {
writeMessage(response);
}
}
}
main().catch(console.error);