codemaxxing 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -6
- package/dist/agent.d.ts +34 -0
- package/dist/agent.js +159 -4
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +6 -1
- package/dist/config.d.ts +2 -0
- package/dist/config.js +9 -0
- package/dist/exec.d.ts +7 -0
- package/dist/exec.js +164 -0
- package/dist/index.js +168 -4
- package/dist/utils/context.d.ts +9 -1
- package/dist/utils/context.js +31 -11
- package/dist/utils/lint.d.ts +13 -0
- package/dist/utils/lint.js +108 -0
- package/dist/utils/mcp.d.ts +55 -0
- package/dist/utils/mcp.js +251 -0
- package/package.json +2 -1
- package/src/agent.ts +179 -4
- package/src/cli.ts +5 -1
- package/src/config.ts +11 -0
- package/src/exec.ts +183 -0
- package/src/index.tsx +167 -3
- package/src/utils/context.ts +34 -12
- package/src/utils/lint.ts +116 -0
- package/src/utils/mcp.ts +307 -0
package/src/utils/mcp.ts
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP (Model Context Protocol) client support
|
|
3
|
+
* Connects to external MCP servers and exposes their tools to the LLM agent.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
7
|
+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
8
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "fs";
|
|
9
|
+
import { join } from "path";
|
|
10
|
+
import { homedir } from "os";
|
|
11
|
+
import type { ChatCompletionTool } from "openai/resources/chat/completions";
|
|
12
|
+
|
|
13
|
+
// ── Types ──
|
|
14
|
+
|
|
15
|
+
export interface MCPServerConfig {
|
|
16
|
+
command: string;
|
|
17
|
+
args?: string[];
|
|
18
|
+
env?: Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface MCPConfig {
|
|
22
|
+
mcpServers: Record<string, MCPServerConfig>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ConnectedServer {
|
|
26
|
+
name: string;
|
|
27
|
+
client: Client;
|
|
28
|
+
transport: StdioClientTransport;
|
|
29
|
+
tools: Array<{ name: string; description?: string; inputSchema: Record<string, unknown> }>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ── Config paths ──
|
|
33
|
+
|
|
34
|
+
const GLOBAL_CONFIG_DIR = join(homedir(), ".codemaxxing");
|
|
35
|
+
const GLOBAL_CONFIG_PATH = join(GLOBAL_CONFIG_DIR, "mcp.json");
|
|
36
|
+
|
|
37
|
+
function getProjectConfigPaths(cwd: string): string[] {
|
|
38
|
+
return [
|
|
39
|
+
join(cwd, ".codemaxxing", "mcp.json"),
|
|
40
|
+
join(cwd, ".cursor", "mcp.json"),
|
|
41
|
+
join(cwd, "opencode.json"),
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// ── Config loading ──
|
|
46
|
+
|
|
47
|
+
function loadConfigFile(path: string): MCPConfig | null {
|
|
48
|
+
try {
|
|
49
|
+
if (!existsSync(path)) return null;
|
|
50
|
+
const raw = readFileSync(path, "utf-8");
|
|
51
|
+
const parsed = JSON.parse(raw);
|
|
52
|
+
if (parsed.mcpServers && typeof parsed.mcpServers === "object") {
|
|
53
|
+
return parsed as MCPConfig;
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
} catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function loadMCPConfig(cwd: string): MCPConfig {
|
|
62
|
+
const merged: MCPConfig = { mcpServers: {} };
|
|
63
|
+
|
|
64
|
+
// Load global config first (lower priority)
|
|
65
|
+
const globalConfig = loadConfigFile(GLOBAL_CONFIG_PATH);
|
|
66
|
+
if (globalConfig) {
|
|
67
|
+
Object.assign(merged.mcpServers, globalConfig.mcpServers);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Load project configs (higher priority — later overwrites earlier)
|
|
71
|
+
for (const configPath of getProjectConfigPaths(cwd)) {
|
|
72
|
+
const config = loadConfigFile(configPath);
|
|
73
|
+
if (config) {
|
|
74
|
+
Object.assign(merged.mcpServers, config.mcpServers);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return merged;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ── Connection management ──
|
|
82
|
+
|
|
83
|
+
const connectedServers: ConnectedServer[] = [];
|
|
84
|
+
|
|
85
|
+
export async function connectToServers(
|
|
86
|
+
config: MCPConfig,
|
|
87
|
+
onStatus?: (name: string, status: string) => void,
|
|
88
|
+
): Promise<ConnectedServer[]> {
|
|
89
|
+
const entries = Object.entries(config.mcpServers);
|
|
90
|
+
if (entries.length === 0) return [];
|
|
91
|
+
|
|
92
|
+
for (const [name, serverConfig] of entries) {
|
|
93
|
+
try {
|
|
94
|
+
onStatus?.(name, "connecting");
|
|
95
|
+
|
|
96
|
+
const transport = new StdioClientTransport({
|
|
97
|
+
command: serverConfig.command,
|
|
98
|
+
args: serverConfig.args ?? [],
|
|
99
|
+
env: { ...process.env, ...(serverConfig.env ?? {}) } as Record<string, string>,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const client = new Client({
|
|
103
|
+
name: "codemaxxing",
|
|
104
|
+
version: "0.3.0",
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
await client.connect(transport);
|
|
108
|
+
|
|
109
|
+
// Fetch available tools
|
|
110
|
+
const toolsResult = await client.listTools();
|
|
111
|
+
const tools = (toolsResult.tools ?? []).map((t) => ({
|
|
112
|
+
name: t.name,
|
|
113
|
+
description: t.description,
|
|
114
|
+
inputSchema: (t.inputSchema ?? { type: "object", properties: {} }) as Record<string, unknown>,
|
|
115
|
+
}));
|
|
116
|
+
|
|
117
|
+
const server: ConnectedServer = { name, client, transport, tools };
|
|
118
|
+
connectedServers.push(server);
|
|
119
|
+
onStatus?.(name, `connected (${tools.length} tools)`);
|
|
120
|
+
} catch (err: any) {
|
|
121
|
+
onStatus?.(name, `failed: ${err.message}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return connectedServers;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export async function disconnectAll(): Promise<void> {
|
|
129
|
+
for (const server of connectedServers) {
|
|
130
|
+
try {
|
|
131
|
+
await server.client.close();
|
|
132
|
+
} catch {
|
|
133
|
+
// Ignore cleanup errors
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
connectedServers.length = 0;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function getConnectedServers(): ConnectedServer[] {
|
|
140
|
+
return connectedServers;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ── Tool format conversion ──
|
|
144
|
+
|
|
145
|
+
export function getAllMCPTools(servers: ConnectedServer[]): ChatCompletionTool[] {
|
|
146
|
+
const tools: ChatCompletionTool[] = [];
|
|
147
|
+
|
|
148
|
+
for (const server of servers) {
|
|
149
|
+
for (const tool of server.tools) {
|
|
150
|
+
tools.push({
|
|
151
|
+
type: "function",
|
|
152
|
+
function: {
|
|
153
|
+
name: `mcp_${server.name}_${tool.name}`,
|
|
154
|
+
description: `[MCP: ${server.name}] ${tool.description ?? tool.name}`,
|
|
155
|
+
parameters: tool.inputSchema as any,
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return tools;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Parse an MCP tool call name to extract server name and tool name.
|
|
166
|
+
* Format: mcp_<serverName>_<toolName>
|
|
167
|
+
* Server names can contain hyphens but not underscores (by convention).
|
|
168
|
+
*/
|
|
169
|
+
export function parseMCPToolName(fullName: string): { serverName: string; toolName: string } | null {
|
|
170
|
+
if (!fullName.startsWith("mcp_")) return null;
|
|
171
|
+
const rest = fullName.slice(4); // Remove "mcp_"
|
|
172
|
+
|
|
173
|
+
// Find the server by matching known connected server names
|
|
174
|
+
for (const server of connectedServers) {
|
|
175
|
+
const prefix = server.name + "_";
|
|
176
|
+
if (rest.startsWith(prefix)) {
|
|
177
|
+
return { serverName: server.name, toolName: rest.slice(prefix.length) };
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Fallback: split on first underscore
|
|
182
|
+
const idx = rest.indexOf("_");
|
|
183
|
+
if (idx === -1) return null;
|
|
184
|
+
return { serverName: rest.slice(0, idx), toolName: rest.slice(idx + 1) };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ── Tool execution ──
|
|
188
|
+
|
|
189
|
+
export async function callMCPTool(
|
|
190
|
+
serverName: string,
|
|
191
|
+
toolName: string,
|
|
192
|
+
args: Record<string, unknown>,
|
|
193
|
+
): Promise<string> {
|
|
194
|
+
const server = connectedServers.find((s) => s.name === serverName);
|
|
195
|
+
if (!server) {
|
|
196
|
+
return `Error: MCP server "${serverName}" not found or not connected.`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
const result = await server.client.callTool({ name: toolName, arguments: args });
|
|
201
|
+
// MCP tool results have a content array
|
|
202
|
+
const content = result.content;
|
|
203
|
+
if (Array.isArray(content)) {
|
|
204
|
+
return content
|
|
205
|
+
.map((c: any) => {
|
|
206
|
+
if (c.type === "text") return c.text;
|
|
207
|
+
if (c.type === "image") return `[image: ${c.mimeType}]`;
|
|
208
|
+
return JSON.stringify(c);
|
|
209
|
+
})
|
|
210
|
+
.join("\n");
|
|
211
|
+
}
|
|
212
|
+
return typeof content === "string" ? content : JSON.stringify(content);
|
|
213
|
+
} catch (err: any) {
|
|
214
|
+
return `Error calling MCP tool "${toolName}" on server "${serverName}": ${err.message}`;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ── Server management ──
|
|
219
|
+
|
|
220
|
+
export function addServer(name: string, config: MCPServerConfig): { ok: boolean; message: string } {
|
|
221
|
+
try {
|
|
222
|
+
if (!existsSync(GLOBAL_CONFIG_DIR)) {
|
|
223
|
+
mkdirSync(GLOBAL_CONFIG_DIR, { recursive: true });
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let existing: MCPConfig = { mcpServers: {} };
|
|
227
|
+
if (existsSync(GLOBAL_CONFIG_PATH)) {
|
|
228
|
+
try {
|
|
229
|
+
existing = JSON.parse(readFileSync(GLOBAL_CONFIG_PATH, "utf-8"));
|
|
230
|
+
if (!existing.mcpServers) existing.mcpServers = {};
|
|
231
|
+
} catch {
|
|
232
|
+
existing = { mcpServers: {} };
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
existing.mcpServers[name] = config;
|
|
237
|
+
writeFileSync(GLOBAL_CONFIG_PATH, JSON.stringify(existing, null, 2) + "\n", "utf-8");
|
|
238
|
+
return { ok: true, message: `Added MCP server "${name}" to global config.` };
|
|
239
|
+
} catch (err: any) {
|
|
240
|
+
return { ok: false, message: `Failed to add server: ${err.message}` };
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function removeServer(name: string): { ok: boolean; message: string } {
|
|
245
|
+
try {
|
|
246
|
+
if (!existsSync(GLOBAL_CONFIG_PATH)) {
|
|
247
|
+
return { ok: false, message: `No global MCP config found.` };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const existing: MCPConfig = JSON.parse(readFileSync(GLOBAL_CONFIG_PATH, "utf-8"));
|
|
251
|
+
if (!existing.mcpServers || !existing.mcpServers[name]) {
|
|
252
|
+
return { ok: false, message: `Server "${name}" not found in global config.` };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
delete existing.mcpServers[name];
|
|
256
|
+
writeFileSync(GLOBAL_CONFIG_PATH, JSON.stringify(existing, null, 2) + "\n", "utf-8");
|
|
257
|
+
return { ok: true, message: `Removed MCP server "${name}" from global config.` };
|
|
258
|
+
} catch (err: any) {
|
|
259
|
+
return { ok: false, message: `Failed to remove server: ${err.message}` };
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export function listServers(cwd: string): Array<{ name: string; source: string; command: string; connected: boolean; toolCount: number }> {
|
|
264
|
+
const result: Array<{ name: string; source: string; command: string; connected: boolean; toolCount: number }> = [];
|
|
265
|
+
|
|
266
|
+
// Gather from global config
|
|
267
|
+
const globalConfig = loadConfigFile(GLOBAL_CONFIG_PATH);
|
|
268
|
+
if (globalConfig) {
|
|
269
|
+
for (const [name, cfg] of Object.entries(globalConfig.mcpServers)) {
|
|
270
|
+
const connected = connectedServers.find((s) => s.name === name);
|
|
271
|
+
result.push({
|
|
272
|
+
name,
|
|
273
|
+
source: "global",
|
|
274
|
+
command: `${cfg.command} ${(cfg.args ?? []).join(" ")}`.trim(),
|
|
275
|
+
connected: !!connected,
|
|
276
|
+
toolCount: connected?.tools.length ?? 0,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Gather from project configs
|
|
282
|
+
for (const configPath of getProjectConfigPaths(cwd)) {
|
|
283
|
+
const config = loadConfigFile(configPath);
|
|
284
|
+
if (config) {
|
|
285
|
+
const source = configPath.includes(".cursor") ? "cursor" : configPath.includes("opencode") ? "opencode" : "project";
|
|
286
|
+
for (const [name, cfg] of Object.entries(config.mcpServers)) {
|
|
287
|
+
// Skip if already listed from global (project overrides)
|
|
288
|
+
const existing = result.find((r) => r.name === name);
|
|
289
|
+
if (existing) {
|
|
290
|
+
existing.source = source;
|
|
291
|
+
existing.command = `${cfg.command} ${(cfg.args ?? []).join(" ")}`.trim();
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
const connected = connectedServers.find((s) => s.name === name);
|
|
295
|
+
result.push({
|
|
296
|
+
name,
|
|
297
|
+
source,
|
|
298
|
+
command: `${cfg.command} ${(cfg.args ?? []).join(" ")}`.trim(),
|
|
299
|
+
connected: !!connected,
|
|
300
|
+
toolCount: connected?.tools.length ?? 0,
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return result;
|
|
307
|
+
}
|