@townco/cli 0.1.23 → 0.1.24
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/dist/index.js +24 -33
- package/package.json +5 -5
- package/dist/commands/mcp-add.d.ts +0 -9
- package/dist/commands/mcp-add.js +0 -494
- package/dist/commands/mcp-list.d.ts +0 -3
- package/dist/commands/mcp-list.js +0 -63
- package/dist/commands/mcp-remove.d.ts +0 -3
- package/dist/commands/mcp-remove.js +0 -120
- package/dist/commands/tool-stub.d.ts +0 -6
- package/dist/commands/tool-stub.js +0 -376
- package/dist/lib/mcp-storage.d.ts +0 -32
- package/dist/lib/mcp-storage.js +0 -111
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { Box, render, Text, useApp, useInput } from "ink";
|
|
3
|
-
import { useEffect, useState } from "react";
|
|
4
|
-
import { listMCPConfigs } from "../lib/mcp-storage";
|
|
5
|
-
// ============================================================================
|
|
6
|
-
// Main Component
|
|
7
|
-
// ============================================================================
|
|
8
|
-
function MCPListApp() {
|
|
9
|
-
const [result, setResult] = useState(null);
|
|
10
|
-
const [loading, setLoading] = useState(true);
|
|
11
|
-
const { exit } = useApp();
|
|
12
|
-
useEffect(() => {
|
|
13
|
-
function loadConfigs() {
|
|
14
|
-
try {
|
|
15
|
-
const configs = listMCPConfigs();
|
|
16
|
-
setResult({ configs });
|
|
17
|
-
}
|
|
18
|
-
catch (error) {
|
|
19
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
20
|
-
setResult({ configs: [], error: errorMsg });
|
|
21
|
-
}
|
|
22
|
-
finally {
|
|
23
|
-
setLoading(false);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
loadConfigs();
|
|
27
|
-
}, []);
|
|
28
|
-
// Exit on any key press when not loading
|
|
29
|
-
useInput((_input, key) => {
|
|
30
|
-
if (!loading) {
|
|
31
|
-
if (key.return || key.escape || _input === "q") {
|
|
32
|
-
exit();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
if (loading) {
|
|
37
|
-
return (_jsx(Box, { children: _jsx(Text, { children: "Loading MCP servers..." }) }));
|
|
38
|
-
}
|
|
39
|
-
if (result?.error) {
|
|
40
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { bold: true, color: "red", children: "\u274C Error loading MCP servers" }) }), _jsx(Box, { marginBottom: 1, children: _jsx(Text, { children: result.error }) }), _jsx(Box, { children: _jsx(Text, { dimColor: true, children: "Press Enter or Q to exit" }) })] }));
|
|
41
|
-
}
|
|
42
|
-
if (!result || result.configs.length === 0) {
|
|
43
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { bold: true, children: "No MCP servers configured" }) }), _jsx(Box, { marginBottom: 1, children: _jsx(Text, { dimColor: true, children: "Add one with: town mcp add" }) }), _jsx(Box, { children: _jsx(Text, { dimColor: true, children: "Press Enter or Q to exit" }) })] }));
|
|
44
|
-
}
|
|
45
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, children: _jsxs(Text, { bold: true, children: ["Configured MCP Servers (", result.configs.length, ")"] }) }), result.configs.map((config, index) => (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Box, { children: _jsxs(Text, { bold: true, color: "cyan", children: [index + 1, ". ", config.name] }) }), _jsx(Box, { paddingLeft: 3, children: config.transport === "http" ? (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { children: ["Transport: ", _jsx(Text, { color: "green", children: "HTTP" })] }), _jsxs(Text, { children: ["URL: ", config.url] }), config.headers && Object.keys(config.headers).length > 0 && (_jsxs(Text, { children: ["Headers:", " ", Object.entries(config.headers)
|
|
46
|
-
.map(([key, value]) => `${key}: ${value}`)
|
|
47
|
-
.join(", ")] }))] })) : (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { children: ["Transport: ", _jsx(Text, { color: "blue", children: "stdio" })] }), _jsxs(Text, { children: ["Command: ", config.command] }), config.args && config.args.length > 0 && (_jsxs(Text, { children: ["Args: ", config.args.join(" ")] }))] })) })] }, config.name))), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Use `town mcp remove` to remove a server or `town mcp add` to add one" }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Press Enter or Q to exit" }) })] }));
|
|
48
|
-
}
|
|
49
|
-
// ============================================================================
|
|
50
|
-
// Export and Runner
|
|
51
|
-
// ============================================================================
|
|
52
|
-
export default MCPListApp;
|
|
53
|
-
export async function runMCPList() {
|
|
54
|
-
const { waitUntilExit, clear } = render(_jsx(MCPListApp, {}));
|
|
55
|
-
try {
|
|
56
|
-
await waitUntilExit();
|
|
57
|
-
}
|
|
58
|
-
finally {
|
|
59
|
-
clear();
|
|
60
|
-
// Ensure cursor is visible
|
|
61
|
-
process.stdout.write("\x1B[?25h");
|
|
62
|
-
}
|
|
63
|
-
}
|
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { SingleSelect } from "@townco/ui/tui";
|
|
3
|
-
import { Box, render, Text } from "ink";
|
|
4
|
-
import { useEffect, useState } from "react";
|
|
5
|
-
import { deleteMCPConfig, getMCPSummary, listMCPConfigs, } from "../lib/mcp-storage";
|
|
6
|
-
// ============================================================================
|
|
7
|
-
// Main Component
|
|
8
|
-
// ============================================================================
|
|
9
|
-
function MCPRemoveApp() {
|
|
10
|
-
const [stage, setStage] = useState("loading");
|
|
11
|
-
const [configs, setConfigs] = useState([]);
|
|
12
|
-
const [selectedName, setSelectedName] = useState(null);
|
|
13
|
-
const [selectedConfig, setSelectedConfig] = useState(null);
|
|
14
|
-
const [errorMessage, setErrorMessage] = useState("");
|
|
15
|
-
// Load configs on mount
|
|
16
|
-
useEffect(() => {
|
|
17
|
-
function loadConfigs() {
|
|
18
|
-
try {
|
|
19
|
-
const configList = listMCPConfigs();
|
|
20
|
-
setConfigs(configList);
|
|
21
|
-
setStage(configList.length > 0 ? "select" : "error");
|
|
22
|
-
if (configList.length === 0) {
|
|
23
|
-
setErrorMessage("No MCP servers configured");
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
catch (error) {
|
|
27
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
28
|
-
setErrorMessage(errorMsg);
|
|
29
|
-
setStage("error");
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
loadConfigs();
|
|
33
|
-
}, []);
|
|
34
|
-
// Handle removal
|
|
35
|
-
const handleRemove = () => {
|
|
36
|
-
if (!selectedName)
|
|
37
|
-
return;
|
|
38
|
-
setStage("removing");
|
|
39
|
-
try {
|
|
40
|
-
const success = deleteMCPConfig(selectedName);
|
|
41
|
-
if (success) {
|
|
42
|
-
setStage("done");
|
|
43
|
-
// Exit immediately
|
|
44
|
-
process.exit(0);
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
throw new Error("Failed to delete MCP config");
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
52
|
-
setErrorMessage(errorMsg);
|
|
53
|
-
setStage("error");
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
if (stage === "loading") {
|
|
57
|
-
return (_jsx(Box, { children: _jsx(Text, { children: "Loading MCP servers..." }) }));
|
|
58
|
-
}
|
|
59
|
-
if (stage === "error") {
|
|
60
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { bold: true, color: "red", children: "\u274C Error" }) }), _jsx(Box, { children: _jsx(Text, { children: errorMessage }) })] }));
|
|
61
|
-
}
|
|
62
|
-
if (stage === "select") {
|
|
63
|
-
const options = configs.map((config) => ({
|
|
64
|
-
label: config.name,
|
|
65
|
-
value: config.name,
|
|
66
|
-
description: getMCPSummary(config),
|
|
67
|
-
}));
|
|
68
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { bold: true, children: "Select MCP server to remove" }) }), _jsx(SingleSelect, { options: options, selected: selectedName, onChange: setSelectedName, onSubmit: (name) => {
|
|
69
|
-
const found = configs.find((c) => c.name === name);
|
|
70
|
-
if (found) {
|
|
71
|
-
setSelectedName(name);
|
|
72
|
-
setSelectedConfig(found);
|
|
73
|
-
setStage("confirm");
|
|
74
|
-
}
|
|
75
|
-
}, onCancel: () => process.exit(0) })] }));
|
|
76
|
-
}
|
|
77
|
-
if (stage === "confirm") {
|
|
78
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { bold: true, children: "Confirm removal" }) }), _jsx(Box, { marginBottom: 1, children: _jsxs(Text, { children: ["Are you sure you want to remove:", " ", _jsx(Text, { bold: true, children: selectedConfig?.name })] }) }), _jsx(Box, { marginBottom: 1, children: _jsx(Text, { dimColor: true, children: getMCPSummary(selectedConfig) }) }), _jsx(SingleSelect, { options: [
|
|
79
|
-
{
|
|
80
|
-
label: "Yes, remove it",
|
|
81
|
-
value: "yes",
|
|
82
|
-
description: "Permanently delete this MCP server configuration",
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
label: "No, cancel",
|
|
86
|
-
value: "no",
|
|
87
|
-
description: "Go back to selection",
|
|
88
|
-
},
|
|
89
|
-
], selected: null, onChange: () => { }, onSubmit: (choice) => {
|
|
90
|
-
if (choice === "yes") {
|
|
91
|
-
handleRemove();
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
setStage("select");
|
|
95
|
-
}
|
|
96
|
-
}, onCancel: () => setStage("select") })] }));
|
|
97
|
-
}
|
|
98
|
-
if (stage === "removing") {
|
|
99
|
-
return (_jsx(Box, { children: _jsx(Text, { children: "\uD83D\uDDD1\uFE0F Removing MCP server..." }) }));
|
|
100
|
-
}
|
|
101
|
-
if (stage === "done") {
|
|
102
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { bold: true, color: "green", children: "\u2705 MCP server removed successfully" }) }), _jsx(Box, { children: _jsxs(Text, { children: ["Removed: ", _jsx(Text, { bold: true, children: selectedConfig?.name })] }) })] }));
|
|
103
|
-
}
|
|
104
|
-
return _jsxs(Text, { children: ["Unknown stage: ", stage] });
|
|
105
|
-
}
|
|
106
|
-
// ============================================================================
|
|
107
|
-
// Export and Runner
|
|
108
|
-
// ============================================================================
|
|
109
|
-
export default MCPRemoveApp;
|
|
110
|
-
export async function runMCPRemove() {
|
|
111
|
-
const { waitUntilExit, clear } = render(_jsx(MCPRemoveApp, {}));
|
|
112
|
-
try {
|
|
113
|
-
await waitUntilExit();
|
|
114
|
-
}
|
|
115
|
-
finally {
|
|
116
|
-
clear();
|
|
117
|
-
// Ensure cursor is visible
|
|
118
|
-
process.stdout.write("\x1B[?25h");
|
|
119
|
-
}
|
|
120
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
interface ToolStubProps {
|
|
2
|
-
name?: string;
|
|
3
|
-
}
|
|
4
|
-
declare function ToolStubApp({ name: initialName }: ToolStubProps): import("react/jsx-runtime").JSX.Element | null;
|
|
5
|
-
export default ToolStubApp;
|
|
6
|
-
export declare function runToolStub(props?: ToolStubProps): Promise<void>;
|
|
@@ -1,376 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { spawn } from "node:child_process";
|
|
3
|
-
import { copyFileSync, existsSync, readFileSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
5
|
-
import { join } from "node:path";
|
|
6
|
-
import { getAgentPath, listAgents } from "@townco/agent/storage";
|
|
7
|
-
import { MultiSelect } from "@townco/ui/tui";
|
|
8
|
-
import { Box, render, Text, useApp, useInput } from "ink";
|
|
9
|
-
import TextInput from "ink-text-input";
|
|
10
|
-
import { useEffect, useState } from "react";
|
|
11
|
-
import { ensureToolsDir, getToolsDir, saveToolConfig, toolConfigExists, } from "../lib/tool-storage";
|
|
12
|
-
// ============================================================================
|
|
13
|
-
// Helper Functions
|
|
14
|
-
// ============================================================================
|
|
15
|
-
/**
|
|
16
|
-
* Normalize a tool name to kebab-case for use as a filename
|
|
17
|
-
*/
|
|
18
|
-
function normalizeToolName(name) {
|
|
19
|
-
return name
|
|
20
|
-
.trim()
|
|
21
|
-
.toLowerCase()
|
|
22
|
-
.replace(/[^a-z0-9]+/g, "-")
|
|
23
|
-
.replace(/^-+|-+$/g, "");
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Convert kebab-case or snake_case to camelCase for function names
|
|
27
|
-
*/
|
|
28
|
-
function toCamelCase(str) {
|
|
29
|
-
return str.replace(/[-_](.)/g, (_, char) => char.toUpperCase());
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Convert a name to snake_case for the tool name export
|
|
33
|
-
*/
|
|
34
|
-
function toSnakeCase(str) {
|
|
35
|
-
return str
|
|
36
|
-
.trim()
|
|
37
|
-
.toLowerCase()
|
|
38
|
-
.replace(/[^a-z0-9]+/g, "_")
|
|
39
|
-
.replace(/^_+|_+$/g, "");
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Generate the stub template content
|
|
43
|
-
*/
|
|
44
|
-
function generateStubTemplate(toolName) {
|
|
45
|
-
const snakeCaseName = toolName ? toSnakeCase(toolName) : "tool_name";
|
|
46
|
-
const camelCaseName = toolName ? toCamelCase(toolName) : "toolName";
|
|
47
|
-
const description = toolName
|
|
48
|
-
? `Describe what ${toolName} does here`
|
|
49
|
-
: "Describe what your tool does here";
|
|
50
|
-
return `// biome-ignore lint/suspicious/noExplicitAny: .
|
|
51
|
-
export const schema = (z: any) =>
|
|
52
|
-
z.object({
|
|
53
|
-
// Define your input parameters here
|
|
54
|
-
// Example: message: z.string().describe("The message to process"),
|
|
55
|
-
input: z.string().optional(),
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
export const name = "${snakeCaseName}";
|
|
59
|
-
export const description = "${description}";
|
|
60
|
-
|
|
61
|
-
export default function ${camelCaseName}(_input: unknown) {
|
|
62
|
-
// TODO: Implement your tool logic here
|
|
63
|
-
|
|
64
|
-
return "result";
|
|
65
|
-
}
|
|
66
|
-
`;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Open a file in the user's editor and wait for it to close
|
|
70
|
-
*/
|
|
71
|
-
async function openInEditor(filePath) {
|
|
72
|
-
return new Promise((resolve, reject) => {
|
|
73
|
-
// Get editor from environment, fallback to vi
|
|
74
|
-
const editor = process.env.EDITOR || process.env.VISUAL || "vi";
|
|
75
|
-
// Parse editor command (might include args like "code --wait")
|
|
76
|
-
const editorParts = editor.split(" ");
|
|
77
|
-
const editorCommand = editorParts[0] || "vi";
|
|
78
|
-
const editorArgs = [...editorParts.slice(1), filePath];
|
|
79
|
-
const child = spawn(editorCommand, editorArgs, {
|
|
80
|
-
stdio: "inherit",
|
|
81
|
-
});
|
|
82
|
-
child.on("exit", (code) => {
|
|
83
|
-
if (code === 0 || code === null) {
|
|
84
|
-
resolve();
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
reject(new Error(`Editor exited with code ${code}`));
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
child.on("error", (error) => {
|
|
91
|
-
reject(error);
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
function TextInputStage({ title, value, onChange, onSubmit, onCancel, placeholder, }) {
|
|
96
|
-
useInput((_input, key) => {
|
|
97
|
-
if (key.escape) {
|
|
98
|
-
onCancel();
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { bold: true, children: title }) }), _jsxs(Box, { children: [_jsxs(Text, { children: [">", " "] }), _jsx(TextInput, { value: value, onChange: onChange, onSubmit: onSubmit, ...(placeholder && { placeholder }) })] }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Enter: Continue \u2022 Esc: Back" }) })] }));
|
|
102
|
-
}
|
|
103
|
-
function NameInputStage({ value, onChange, onNext, onBack, }) {
|
|
104
|
-
const [localError, setLocalError] = useState(null);
|
|
105
|
-
const handleSubmit = () => {
|
|
106
|
-
const trimmed = value.trim();
|
|
107
|
-
if (!trimmed) {
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
// Check for duplicate
|
|
111
|
-
if (toolConfigExists(trimmed)) {
|
|
112
|
-
setLocalError(`Tool "${trimmed}" already exists`);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
onNext(trimmed);
|
|
116
|
-
};
|
|
117
|
-
const handleChange = (newValue) => {
|
|
118
|
-
if (localError) {
|
|
119
|
-
setLocalError(null);
|
|
120
|
-
}
|
|
121
|
-
onChange(newValue);
|
|
122
|
-
};
|
|
123
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(TextInputStage, { title: "Enter tool name:", value: value, onChange: handleChange, onSubmit: handleSubmit, onCancel: onBack, placeholder: "my-custom-tool" }), localError && (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: "red", children: ["\u274C ", localError] }) }))] }));
|
|
124
|
-
}
|
|
125
|
-
function NoAgentsMessage({ onNext }) {
|
|
126
|
-
useEffect(() => {
|
|
127
|
-
const timer = setTimeout(() => {
|
|
128
|
-
onNext();
|
|
129
|
-
}, 1000);
|
|
130
|
-
return () => clearTimeout(timer);
|
|
131
|
-
}, [onNext]);
|
|
132
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { dimColor: true, children: "No agents found. Tool will be saved globally." }), _jsx(Text, { dimColor: true, children: "You can attach it to agents later." })] }));
|
|
133
|
-
}
|
|
134
|
-
function AgentSelectionStage({ selectedAgents, onSelectedAgentsChange, onNext, onBack, }) {
|
|
135
|
-
const [availableAgents, setAvailableAgents] = useState([]);
|
|
136
|
-
const [isLoading, setIsLoading] = useState(true);
|
|
137
|
-
useEffect(() => {
|
|
138
|
-
// Fetch available agents
|
|
139
|
-
const fetchAgents = async () => {
|
|
140
|
-
try {
|
|
141
|
-
const agents = await listAgents();
|
|
142
|
-
setAvailableAgents(agents);
|
|
143
|
-
}
|
|
144
|
-
catch (error) {
|
|
145
|
-
console.error("Error fetching agents:", error);
|
|
146
|
-
setAvailableAgents([]);
|
|
147
|
-
}
|
|
148
|
-
finally {
|
|
149
|
-
setIsLoading(false);
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
fetchAgents();
|
|
153
|
-
}, []);
|
|
154
|
-
// If still loading, show loading message
|
|
155
|
-
if (isLoading) {
|
|
156
|
-
return (_jsx(Box, { flexDirection: "column", children: _jsx(Text, { children: "Loading agents..." }) }));
|
|
157
|
-
}
|
|
158
|
-
// If no agents available, show info message and auto-proceed
|
|
159
|
-
if (availableAgents.length === 0) {
|
|
160
|
-
return _jsx(NoAgentsMessage, { onNext: onNext });
|
|
161
|
-
}
|
|
162
|
-
// Show agent selection UI
|
|
163
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { bold: true, children: "Register tool with agents (optional):" }) }), _jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { dimColor: true, children: "Select which agents should have access to this tool." }), _jsx(Text, { dimColor: true, children: "You can skip this step and register agents later." })] }), _jsx(MultiSelect, { options: availableAgents.map((agent) => ({
|
|
164
|
-
label: agent,
|
|
165
|
-
value: agent,
|
|
166
|
-
})), selected: selectedAgents, onChange: onSelectedAgentsChange, onSubmit: onNext, onCancel: onBack })] }));
|
|
167
|
-
}
|
|
168
|
-
function DoneStage({ config, status, error, attachedAgents }) {
|
|
169
|
-
const { exit } = useApp();
|
|
170
|
-
useEffect(() => {
|
|
171
|
-
if (status === "done") {
|
|
172
|
-
setImmediate(() => {
|
|
173
|
-
exit();
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
}, [status, exit]);
|
|
177
|
-
if (status === "saving") {
|
|
178
|
-
return (_jsx(Box, { flexDirection: "column", children: _jsx(Text, { children: "\u23F3 Saving tool configuration..." }) }));
|
|
179
|
-
}
|
|
180
|
-
if (status === "error") {
|
|
181
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: "red", children: "\u274C Error saving tool" }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { children: error }) })] }));
|
|
182
|
-
}
|
|
183
|
-
if (status === "done") {
|
|
184
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: "green", children: "\u2705 Tool saved successfully!" }), _jsxs(Box, { marginTop: 1, children: [_jsxs(Text, { dimColor: true, children: ["Name: ", config.name] }), _jsxs(Text, { dimColor: true, children: ["Path: ", config.path] })] }), attachedAgents.length > 0 && (_jsx(Box, { marginTop: 1, flexDirection: "column", children: _jsxs(Text, { dimColor: true, children: ["Registered with agents: ", attachedAgents.join(", ")] }) }))] }));
|
|
185
|
-
}
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
// ============================================================================
|
|
189
|
-
// Main Component
|
|
190
|
-
// ============================================================================
|
|
191
|
-
function ToolStubApp({ name: initialName }) {
|
|
192
|
-
const { exit } = useApp();
|
|
193
|
-
const [stage, setStage] = useState("name");
|
|
194
|
-
const [config, setConfig] = useState({});
|
|
195
|
-
const [nameInput, setNameInput] = useState(initialName || "");
|
|
196
|
-
const [saveStatus, setSaveStatus] = useState("pending");
|
|
197
|
-
const [saveError, setSaveError] = useState(null);
|
|
198
|
-
const [selectedAgents, setSelectedAgents] = useState([]);
|
|
199
|
-
const [tempFilePath, setTempFilePath] = useState(null);
|
|
200
|
-
const [editorOpened, setEditorOpened] = useState(false);
|
|
201
|
-
// On mount, create temp file and open editor
|
|
202
|
-
useEffect(() => {
|
|
203
|
-
if (editorOpened)
|
|
204
|
-
return;
|
|
205
|
-
const setupAndOpenEditor = async () => {
|
|
206
|
-
try {
|
|
207
|
-
// Generate temp file path
|
|
208
|
-
const tempFile = join(tmpdir(), `town-tool-${Date.now()}.ts`);
|
|
209
|
-
setTempFilePath(tempFile);
|
|
210
|
-
// Generate and write stub template
|
|
211
|
-
const stubContent = generateStubTemplate(initialName);
|
|
212
|
-
writeFileSync(tempFile, stubContent, "utf-8");
|
|
213
|
-
// Open in editor
|
|
214
|
-
await openInEditor(tempFile);
|
|
215
|
-
// After editor closes, move to name input stage
|
|
216
|
-
setEditorOpened(true);
|
|
217
|
-
}
|
|
218
|
-
catch (error) {
|
|
219
|
-
setSaveStatus("error");
|
|
220
|
-
setSaveError(`Failed to open editor: ${error instanceof Error ? error.message : String(error)}`);
|
|
221
|
-
setStage("done");
|
|
222
|
-
}
|
|
223
|
-
};
|
|
224
|
-
setupAndOpenEditor();
|
|
225
|
-
}, [initialName, editorOpened]);
|
|
226
|
-
const handleSave = (toolName, agentsToAttach) => {
|
|
227
|
-
setSaveStatus("saving");
|
|
228
|
-
try {
|
|
229
|
-
if (!tempFilePath) {
|
|
230
|
-
setSaveStatus("error");
|
|
231
|
-
setSaveError("Temp file path not found");
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
// Check if temp file still exists
|
|
235
|
-
if (!existsSync(tempFilePath)) {
|
|
236
|
-
setSaveStatus("error");
|
|
237
|
-
setSaveError("Temp file was deleted");
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
// Read the edited content
|
|
241
|
-
const editedContent = readFileSync(tempFilePath, "utf-8");
|
|
242
|
-
// Ensure tools directory exists
|
|
243
|
-
ensureToolsDir();
|
|
244
|
-
// Normalize the tool name for the filename
|
|
245
|
-
const normalizedName = normalizeToolName(toolName);
|
|
246
|
-
const toolsDir = getToolsDir();
|
|
247
|
-
const targetPath = join(toolsDir, `${normalizedName}.ts`);
|
|
248
|
-
// Write the edited content to the target path
|
|
249
|
-
try {
|
|
250
|
-
writeFileSync(targetPath, editedContent, "utf-8");
|
|
251
|
-
}
|
|
252
|
-
catch (error) {
|
|
253
|
-
setSaveStatus("error");
|
|
254
|
-
setSaveError(`Failed to save file: ${error instanceof Error ? error.message : String(error)}`);
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
// Create tool config
|
|
258
|
-
const toolConfig = {
|
|
259
|
-
name: toolName,
|
|
260
|
-
path: targetPath,
|
|
261
|
-
};
|
|
262
|
-
// Save to global tool storage
|
|
263
|
-
saveToolConfig(toolConfig);
|
|
264
|
-
// Update selected agents
|
|
265
|
-
for (const agentName of agentsToAttach) {
|
|
266
|
-
try {
|
|
267
|
-
const agentPath = getAgentPath(agentName);
|
|
268
|
-
const agentJsonPath = join(agentPath, "agent.json");
|
|
269
|
-
// Read existing agent.json
|
|
270
|
-
const agentJsonContent = readFileSync(agentJsonPath, "utf-8");
|
|
271
|
-
const agentDef = JSON.parse(agentJsonContent);
|
|
272
|
-
// Add tool to tools array (create array if doesn't exist)
|
|
273
|
-
if (!agentDef.tools) {
|
|
274
|
-
agentDef.tools = [];
|
|
275
|
-
}
|
|
276
|
-
// Create custom tool object
|
|
277
|
-
const customTool = {
|
|
278
|
-
type: "custom",
|
|
279
|
-
modulePath: targetPath,
|
|
280
|
-
};
|
|
281
|
-
// Check if this tool is already in the agent's config
|
|
282
|
-
const existingIndex = agentDef.tools.findIndex((tool) => typeof tool === "object" &&
|
|
283
|
-
tool !== null &&
|
|
284
|
-
"type" in tool &&
|
|
285
|
-
tool.type === "custom" &&
|
|
286
|
-
"modulePath" in tool &&
|
|
287
|
-
tool.modulePath === targetPath);
|
|
288
|
-
if (existingIndex >= 0) {
|
|
289
|
-
// Update existing config
|
|
290
|
-
agentDef.tools[existingIndex] = customTool;
|
|
291
|
-
}
|
|
292
|
-
else {
|
|
293
|
-
// Add new config
|
|
294
|
-
agentDef.tools.push(customTool);
|
|
295
|
-
}
|
|
296
|
-
// Write back to agent.json
|
|
297
|
-
writeFileSync(agentJsonPath, JSON.stringify(agentDef, null, 2));
|
|
298
|
-
}
|
|
299
|
-
catch (error) {
|
|
300
|
-
console.error(`Error updating agent ${agentName}:`, error);
|
|
301
|
-
// Continue with other agents even if one fails
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
// Clean up temp file
|
|
305
|
-
try {
|
|
306
|
-
unlinkSync(tempFilePath);
|
|
307
|
-
}
|
|
308
|
-
catch (error) {
|
|
309
|
-
// Ignore cleanup errors
|
|
310
|
-
}
|
|
311
|
-
setConfig(toolConfig);
|
|
312
|
-
setSaveStatus("done");
|
|
313
|
-
}
|
|
314
|
-
catch (error) {
|
|
315
|
-
setSaveStatus("error");
|
|
316
|
-
setSaveError(error instanceof Error ? error.message : "Unknown error occurred");
|
|
317
|
-
}
|
|
318
|
-
};
|
|
319
|
-
// Wait for editor to open and close before showing UI
|
|
320
|
-
if (!editorOpened) {
|
|
321
|
-
return (_jsx(Box, { flexDirection: "column", children: _jsx(Text, { children: "Opening editor..." }) }));
|
|
322
|
-
}
|
|
323
|
-
// If there was an error during editor setup, show done stage with error
|
|
324
|
-
if (saveStatus === "error" && stage === "done") {
|
|
325
|
-
return (_jsx(DoneStage, { config: config, status: saveStatus, error: saveError, attachedAgents: selectedAgents }));
|
|
326
|
-
}
|
|
327
|
-
// Name input stage
|
|
328
|
-
if (stage === "name") {
|
|
329
|
-
return (_jsx(NameInputStage, { value: nameInput, onChange: setNameInput, onNext: (name) => {
|
|
330
|
-
setConfig({ ...config, name });
|
|
331
|
-
setStage("agentSelection");
|
|
332
|
-
}, onBack: () => {
|
|
333
|
-
// Clean up temp file
|
|
334
|
-
if (tempFilePath) {
|
|
335
|
-
try {
|
|
336
|
-
unlinkSync(tempFilePath);
|
|
337
|
-
}
|
|
338
|
-
catch (error) {
|
|
339
|
-
// Ignore cleanup errors
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
exit();
|
|
343
|
-
} }));
|
|
344
|
-
}
|
|
345
|
-
// Agent selection stage
|
|
346
|
-
if (stage === "agentSelection") {
|
|
347
|
-
return (_jsx(AgentSelectionStage, { selectedAgents: selectedAgents, onSelectedAgentsChange: setSelectedAgents, onNext: () => {
|
|
348
|
-
if (!config.name) {
|
|
349
|
-
setSaveStatus("error");
|
|
350
|
-
setSaveError("Tool name is required");
|
|
351
|
-
setStage("done");
|
|
352
|
-
return;
|
|
353
|
-
}
|
|
354
|
-
handleSave(config.name, selectedAgents);
|
|
355
|
-
setStage("done");
|
|
356
|
-
}, onBack: () => setStage("name") }));
|
|
357
|
-
}
|
|
358
|
-
// Done stage
|
|
359
|
-
if (stage === "done") {
|
|
360
|
-
return (_jsx(DoneStage, { config: config, status: saveStatus, error: saveError, attachedAgents: selectedAgents }));
|
|
361
|
-
}
|
|
362
|
-
return null;
|
|
363
|
-
}
|
|
364
|
-
// ============================================================================
|
|
365
|
-
// Export and Runner
|
|
366
|
-
// ============================================================================
|
|
367
|
-
export default ToolStubApp;
|
|
368
|
-
export async function runToolStub(props = {}) {
|
|
369
|
-
// Set stdin to raw mode to capture input
|
|
370
|
-
if (process.stdin.isTTY) {
|
|
371
|
-
process.stdin.setRawMode(true);
|
|
372
|
-
}
|
|
373
|
-
const { waitUntilExit } = render(_jsx(ToolStubApp, { ...props }));
|
|
374
|
-
// Wait for the app to exit before returning
|
|
375
|
-
await waitUntilExit();
|
|
376
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export type MCPConfig = {
|
|
2
|
-
name: string;
|
|
3
|
-
transport: "stdio" | "http";
|
|
4
|
-
command?: string;
|
|
5
|
-
args?: string[];
|
|
6
|
-
url?: string;
|
|
7
|
-
headers?: Record<string, string>;
|
|
8
|
-
};
|
|
9
|
-
/**
|
|
10
|
-
* Save an MCP config to the store
|
|
11
|
-
*/
|
|
12
|
-
export declare function saveMCPConfig(config: MCPConfig): void;
|
|
13
|
-
/**
|
|
14
|
-
* Load an MCP config by name
|
|
15
|
-
*/
|
|
16
|
-
export declare function loadMCPConfig(name: string): MCPConfig | null;
|
|
17
|
-
/**
|
|
18
|
-
* Delete an MCP config by name
|
|
19
|
-
*/
|
|
20
|
-
export declare function deleteMCPConfig(name: string): boolean;
|
|
21
|
-
/**
|
|
22
|
-
* List all MCP configs
|
|
23
|
-
*/
|
|
24
|
-
export declare function listMCPConfigs(): MCPConfig[];
|
|
25
|
-
/**
|
|
26
|
-
* Check if an MCP config exists
|
|
27
|
-
*/
|
|
28
|
-
export declare function mcpConfigExists(name: string): boolean;
|
|
29
|
-
/**
|
|
30
|
-
* Get a summary of an MCP config for display
|
|
31
|
-
*/
|
|
32
|
-
export declare function getMCPSummary(config: MCPConfig): string;
|