@mrciphersmith/roomyx 0.4.0 → 0.6.0
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/CHANGELOG.md +432 -0
- package/README.md +85 -17
- package/package.json +5 -4
- package/src/bin/launch.mjs +68 -0
- package/src/bin/roomyx-client.mjs +8 -0
- package/src/bin/roomyx.mjs +4 -0
- package/src/cli/args.ts +160 -0
- package/src/cli/specs.ts +16 -0
- package/src/cli.ts +361 -150
- package/src/client/components/footer.ts +53 -0
- package/src/client/components/help-overlay.ts +124 -0
- package/src/client/components/message-row.ts +71 -6
- package/src/client/components/roster-sidebar.ts +57 -3
- package/src/client/components/status-bar.ts +5 -1
- package/src/client/export-name.ts +57 -0
- package/src/client/index.ts +271 -49
- package/src/client/keymap.ts +157 -0
- package/src/client/liveness.ts +56 -0
- package/src/client/mcp-client.ts +142 -32
- package/src/client/owner-prompt.ts +9 -2
- package/src/client/screens/chat-view.ts +172 -10
- package/src/client/search-prompt.ts +67 -0
- package/src/client/shutdown.ts +39 -0
- package/src/client/transcript.ts +124 -0
- package/src/installer/init.ts +12 -2
- package/src/installer/registry.ts +39 -80
- package/src/installer/skill-sync.ts +13 -6
- package/src/lockfile.ts +85 -0
- package/src/log/schema.ts +59 -0
- package/src/log/store.ts +1 -40
- package/src/log/write.ts +127 -0
- package/src/mcp-management/server.ts +39 -101
- package/src/server/http-transport.ts +217 -0
- package/src/server/serve.ts +13 -86
- package/src/cli/seed.ts +0 -73
- package/src/client/screens/agent-modal.ts +0 -59
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { createServer } from "node:http";
|
|
2
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
-
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
4
2
|
import { z } from "zod";
|
|
3
|
+
import { serveMcpOverHttp } from "../server/http-transport";
|
|
4
|
+
import type { McpHttpTransportHandle } from "../server/http-transport";
|
|
5
5
|
import { listLiveRooms } from "../installer/registry";
|
|
6
6
|
import { syncSkill } from "../installer/skill-sync";
|
|
7
7
|
import { NAMED_TARGETS, resolveTargets } from "../installer/skill-targets";
|
|
@@ -10,6 +10,15 @@ export interface ManagementOptions {
|
|
|
10
10
|
registryPath: string;
|
|
11
11
|
bundledSkillPath: string;
|
|
12
12
|
configPath: string;
|
|
13
|
+
/**
|
|
14
|
+
* The project a project-scoped target resolves against — `keryx` keeps its
|
|
15
|
+
* skills in `<project>/.metaproject/project-skills/`. Defaults to
|
|
16
|
+
* `process.cwd()`, which is right for `roomyx mcp` and wrong for a
|
|
17
|
+
* long-lived embedded server whose working directory is somebody else's.
|
|
18
|
+
* Every other path this server touches is already explicit; this one was
|
|
19
|
+
* the exception.
|
|
20
|
+
*/
|
|
21
|
+
cwd?: string;
|
|
13
22
|
}
|
|
14
23
|
|
|
15
24
|
export interface ManagementServeOptions {
|
|
@@ -45,33 +54,25 @@ export function createManagementMcpServer(options: ManagementOptions): McpServer
|
|
|
45
54
|
{
|
|
46
55
|
title: "Sync the bundled startup-room skill",
|
|
47
56
|
description:
|
|
48
|
-
"Diffs/backs-up/writes the bundled skill into a
|
|
57
|
+
"Diffs/backs-up/writes the bundled skill into a named runtime's skill location (see decisions.md D-02 for safety rules).",
|
|
49
58
|
inputSchema: {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
59
|
+
// Named targets only. This tool used to accept a literal `targetPath`,
|
|
60
|
+
// which made it an arbitrary-path file writer reachable over a loopback
|
|
61
|
+
// port with no authentication — a browser on any page the operator
|
|
62
|
+
// visits could drive it cross-origin. The CLI keeps the literal-path
|
|
63
|
+
// escape hatch, because that is the operator on their own machine;
|
|
64
|
+
// the network surface does not get one.
|
|
65
|
+
target: z.enum([...NAMED_TARGETS, "all"]).describe("A runtime by name, or `all`."),
|
|
57
66
|
},
|
|
58
67
|
},
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
text: JSON.stringify({
|
|
68
|
-
error: `Pass either target (${[...NAMED_TARGETS, "all"].join(", ")}) or targetPath.`,
|
|
69
|
-
}),
|
|
70
|
-
},
|
|
71
|
-
],
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
const targets = targetPath ? [{ name: targetPath, path: targetPath }] : resolveTargets(target as string);
|
|
68
|
+
// No `yes`, and no `dryRun`. `yes` is what D-02's overwrite guard turns
|
|
69
|
+
// off, and on the CLI it is an operator's keystroke; over MCP it would be a
|
|
70
|
+
// boolean the caller sets for itself, which is not a guard. This tool
|
|
71
|
+
// reports what a sync *would* do — the deciding is the operator's, at the
|
|
72
|
+
// CLI, where `--yes` means a person decided. Same reasoning that removed
|
|
73
|
+
// `targetPath` from this schema.
|
|
74
|
+
async ({ target }) => {
|
|
75
|
+
const targets = resolveTargets(target, options.cwd);
|
|
75
76
|
const results = targets.map(({ name, path }) => ({
|
|
76
77
|
target: name,
|
|
77
78
|
path,
|
|
@@ -79,8 +80,7 @@ export function createManagementMcpServer(options: ManagementOptions): McpServer
|
|
|
79
80
|
bundledSkillPath: options.bundledSkillPath,
|
|
80
81
|
targetPath: path,
|
|
81
82
|
configPath: options.configPath,
|
|
82
|
-
dryRun,
|
|
83
|
-
yes,
|
|
83
|
+
dryRun: true,
|
|
84
84
|
}),
|
|
85
85
|
}));
|
|
86
86
|
return { content: [{ type: "text", text: JSON.stringify(results) }] };
|
|
@@ -90,86 +90,24 @@ export function createManagementMcpServer(options: ManagementOptions): McpServer
|
|
|
90
90
|
return server;
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
export
|
|
94
|
-
url: string;
|
|
95
|
-
port: number;
|
|
96
|
-
close(): Promise<void>;
|
|
97
|
-
}
|
|
93
|
+
export type ManagementHandle = McpHttpTransportHandle;
|
|
98
94
|
|
|
99
|
-
const LOOPBACK_HOSTS = new Set(["127.0.0.1", "localhost", "::1"]);
|
|
100
95
|
|
|
101
96
|
/**
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
* gets its own transport + McpServer pair.
|
|
97
|
+
* Serves the management tools over the shared loopback HTTP transport. The
|
|
98
|
+
* session, binding and shutdown behaviour is `../server/http-transport`'s —
|
|
99
|
+
* this file used to carry its own copy, and the copies had already drifted.
|
|
106
100
|
*/
|
|
107
101
|
export async function serveManagement(
|
|
108
102
|
options: ManagementOptions,
|
|
109
103
|
serveOptions: ManagementServeOptions,
|
|
110
104
|
): Promise<ManagementHandle> {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const sessions = new Map<string, StreamableHTTPServerTransport>();
|
|
119
|
-
|
|
120
|
-
async function createSession(): Promise<StreamableHTTPServerTransport> {
|
|
121
|
-
const transport = new StreamableHTTPServerTransport({
|
|
122
|
-
sessionIdGenerator: () => crypto.randomUUID(),
|
|
123
|
-
onsessioninitialized: (sessionId) => {
|
|
124
|
-
sessions.set(sessionId, transport);
|
|
125
|
-
},
|
|
126
|
-
onsessionclosed: (sessionId) => {
|
|
127
|
-
sessions.delete(sessionId);
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
|
-
const mcpServer = createManagementMcpServer(options);
|
|
131
|
-
await mcpServer.connect(transport);
|
|
132
|
-
return transport;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const httpServer = createServer((req, res) => {
|
|
136
|
-
const sessionIdHeader = req.headers["mcp-session-id"];
|
|
137
|
-
const sessionId = typeof sessionIdHeader === "string" ? sessionIdHeader : undefined;
|
|
138
|
-
const existing = sessionId ? sessions.get(sessionId) : undefined;
|
|
139
|
-
|
|
140
|
-
const transportPromise = existing ? Promise.resolve(existing) : createSession();
|
|
141
|
-
transportPromise
|
|
142
|
-
.then((transport) => transport.handleRequest(req, res))
|
|
143
|
-
.catch((error) => {
|
|
144
|
-
if (!res.headersSent) res.writeHead(500, { "content-type": "application/json" });
|
|
145
|
-
res.end(JSON.stringify({ error: error instanceof Error ? error.message : String(error) }));
|
|
146
|
-
});
|
|
105
|
+
return serveMcpOverHttp({
|
|
106
|
+
createMcpServer: () => createManagementMcpServer(options),
|
|
107
|
+
commandLabel: "roomyx mcp",
|
|
108
|
+
defaultPort: 4320,
|
|
109
|
+
port: serveOptions.port,
|
|
110
|
+
host: serveOptions.host,
|
|
111
|
+
acknowledgeNonLoopback: serveOptions.acknowledgeNonLoopback,
|
|
147
112
|
});
|
|
148
|
-
|
|
149
|
-
const port = serveOptions.port ?? 4320;
|
|
150
|
-
try {
|
|
151
|
-
await new Promise<void>((resolve, reject) => {
|
|
152
|
-
httpServer.once("error", reject);
|
|
153
|
-
httpServer.listen(port, host, () => resolve());
|
|
154
|
-
});
|
|
155
|
-
} catch (error) {
|
|
156
|
-
await Promise.all([...sessions.values()].map((t) => t.close().catch(() => undefined)));
|
|
157
|
-
throw error;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const address = httpServer.address();
|
|
161
|
-
const actualPort = typeof address === "object" && address !== null ? address.port : port;
|
|
162
|
-
|
|
163
|
-
return {
|
|
164
|
-
url: `http://${host}:${actualPort}/mcp`,
|
|
165
|
-
port: actualPort,
|
|
166
|
-
close: () =>
|
|
167
|
-
new Promise<void>((resolve, reject) => {
|
|
168
|
-
httpServer.close(async (error) => {
|
|
169
|
-
await Promise.all([...sessions.values()].map((t) => t.close().catch(() => undefined)));
|
|
170
|
-
if (error) reject(error);
|
|
171
|
-
else resolve();
|
|
172
|
-
});
|
|
173
|
-
}),
|
|
174
|
-
};
|
|
175
113
|
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { createServer as createHttpServer } from "node:http";
|
|
2
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
3
|
+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The loopback HTTP transport both roomyx servers bind to.
|
|
7
|
+
*
|
|
8
|
+
* It was written twice — once for the per-room server, once for the management
|
|
9
|
+
* server — and the copies had drifted: only one of them closed its sessions on
|
|
10
|
+
* shutdown. Two servers meant every transport-level fix had to be applied
|
|
11
|
+
* twice, and a signed tarball is a poor place to discover that the second one
|
|
12
|
+
* was missed. There is one now.
|
|
13
|
+
*
|
|
14
|
+
* A single `StreamableHTTPServerTransport` only ever completes ONE `initialize`
|
|
15
|
+
* handshake for its whole lifetime — an independent review reproduced this
|
|
16
|
+
* concretely: a second client, even a short-lived liveness check, gets "Server
|
|
17
|
+
* already initialized" and the transport is permanently unusable afterward. So
|
|
18
|
+
* each session (a request with no `mcp-session-id`) gets its own transport and
|
|
19
|
+
* its own `McpServer`, tracked by session id, and later requests carrying that
|
|
20
|
+
* header are routed to the pair that already exists.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export interface McpHttpTransportOptions {
|
|
24
|
+
/** Called once per session. Each session gets its own server instance. */
|
|
25
|
+
createMcpServer: () => McpServer;
|
|
26
|
+
/** Used in the refusal message when a non-loopback host is asked for without acknowledgement. */
|
|
27
|
+
commandLabel: string;
|
|
28
|
+
/** `roomyx serve` uses 4319, `roomyx mcp` 4320 — they must not collide. 0 selects an ephemeral port. */
|
|
29
|
+
defaultPort: number;
|
|
30
|
+
port?: number;
|
|
31
|
+
host?: string;
|
|
32
|
+
acknowledgeNonLoopback?: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface McpHttpTransportHandle {
|
|
36
|
+
url: string;
|
|
37
|
+
port: number;
|
|
38
|
+
close(): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const LOOPBACK_HOSTS = new Set(["127.0.0.1", "localhost", "::1"]);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Refuses non-loopback hosts unless explicitly acknowledged, mirroring `keryx
|
|
45
|
+
* serve`'s convention — same reasoning, recorded as D-06.
|
|
46
|
+
*/
|
|
47
|
+
export async function serveMcpOverHttp(
|
|
48
|
+
options: McpHttpTransportOptions,
|
|
49
|
+
): Promise<McpHttpTransportHandle> {
|
|
50
|
+
const host = options.host ?? "127.0.0.1";
|
|
51
|
+
if (!LOOPBACK_HOSTS.has(host) && !options.acknowledgeNonLoopback) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`Refusing to bind ${options.commandLabel} to non-loopback host "${host}" without --acknowledge-non-loopback.`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Live sessions, with the time each was last used.
|
|
59
|
+
*
|
|
60
|
+
* A session used to leave this map only via `onsessionclosed`, which the SDK
|
|
61
|
+
* fires on an HTTP DELETE — and nothing in roomyx sends one. `Client.close()`
|
|
62
|
+
* aborts the local controller; `terminateSession()` is the method that sends
|
|
63
|
+
* the DELETE, and it had no callers. So every session the process ever
|
|
64
|
+
* accepted was retained for the life of the process, at roughly 70-78 KB
|
|
65
|
+
* each, fed by ordinary use: one per `rooms list`, one per `room append`, one
|
|
66
|
+
* per client start, one per reconnect. Each abandoned id also stayed a fully
|
|
67
|
+
* routable handle to the room — three clients closed, then a raw POST
|
|
68
|
+
* replaying their recorded ids still returned a full `tools/list`.
|
|
69
|
+
*/
|
|
70
|
+
const sessions = new Map<string, { transport: StreamableHTTPServerTransport; lastSeenMs: number }>();
|
|
71
|
+
|
|
72
|
+
// Filled in after listen(). Sessions are only ever created while handling a
|
|
73
|
+
// request, so by the time createSession runs this is the real bound port —
|
|
74
|
+
// which matters because the allowlists below cannot be built without it.
|
|
75
|
+
let boundPort = 0;
|
|
76
|
+
|
|
77
|
+
async function createSession(): Promise<StreamableHTTPServerTransport> {
|
|
78
|
+
const transport = new StreamableHTTPServerTransport({
|
|
79
|
+
sessionIdGenerator: () => crypto.randomUUID(),
|
|
80
|
+
// Both guards, built lazily, from the port we actually bound.
|
|
81
|
+
//
|
|
82
|
+
// `allowedHosts` is an exact match against the whole `Host` header,
|
|
83
|
+
// port included: a port-less allowlist returns 403 to roomyx's own
|
|
84
|
+
// client. And with `--port 0` the port is not knowable until listen()
|
|
85
|
+
// resolves, so an allowlist written at option-construction time would be
|
|
86
|
+
// wrong by construction.
|
|
87
|
+
//
|
|
88
|
+
// Both lists are required because they stop different attacks. A page at
|
|
89
|
+
// http://127.0.0.1:<port>/mcp sends a legitimate Host, so only
|
|
90
|
+
// `allowedOrigins` refuses it. A DNS-rebound page sends
|
|
91
|
+
// `Host: attacker.example` and, post-rebind, a same-origin Origin — so
|
|
92
|
+
// only `allowedHosts` refuses that one.
|
|
93
|
+
//
|
|
94
|
+
// Nothing legitimate sends an Origin at all: roomyx's own client, the
|
|
95
|
+
// liveness probe and every MCP client are not browsers. The SDK skips
|
|
96
|
+
// the Origin check when the header is absent, so the list below exists
|
|
97
|
+
// to be matched by nothing.
|
|
98
|
+
enableDnsRebindingProtection: true,
|
|
99
|
+
allowedHosts: [`127.0.0.1:${boundPort}`, `localhost:${boundPort}`, `[::1]:${boundPort}`],
|
|
100
|
+
allowedOrigins: [`http://127.0.0.1:${boundPort}`, `http://localhost:${boundPort}`],
|
|
101
|
+
onsessioninitialized: (sessionId) => {
|
|
102
|
+
sessions.set(sessionId, { transport, lastSeenMs: Date.now() });
|
|
103
|
+
},
|
|
104
|
+
onsessionclosed: (sessionId) => {
|
|
105
|
+
sessions.delete(sessionId);
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
// The DELETE is one way a session can end; the transport closing for any
|
|
109
|
+
// other reason is another, and only the first was being listened for.
|
|
110
|
+
transport.onclose = () => {
|
|
111
|
+
const id = transport.sessionId;
|
|
112
|
+
if (id !== undefined) sessions.delete(id);
|
|
113
|
+
};
|
|
114
|
+
const mcpServer = options.createMcpServer();
|
|
115
|
+
await mcpServer.connect(transport);
|
|
116
|
+
return transport;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Closes sessions nothing has touched for a while.
|
|
121
|
+
*
|
|
122
|
+
* A well-behaved client polls every second or three, so a minute of silence
|
|
123
|
+
* means it is gone — and the ones that are gone are precisely the ones that
|
|
124
|
+
* never sent the DELETE that would have said so. `unref()` so an idle sweeper
|
|
125
|
+
* cannot be the reason a process stays alive.
|
|
126
|
+
*/
|
|
127
|
+
const IDLE_SESSION_MS = 60_000;
|
|
128
|
+
const sweeper = setInterval(() => {
|
|
129
|
+
const cutoff = Date.now() - IDLE_SESSION_MS;
|
|
130
|
+
for (const [id, entry] of sessions) {
|
|
131
|
+
if (entry.lastSeenMs < cutoff) {
|
|
132
|
+
sessions.delete(id);
|
|
133
|
+
void entry.transport.close().catch(() => undefined);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}, IDLE_SESSION_MS / 2);
|
|
137
|
+
sweeper.unref();
|
|
138
|
+
|
|
139
|
+
const httpServer = createHttpServer((req, res) => {
|
|
140
|
+
const sessionIdHeader = req.headers["mcp-session-id"];
|
|
141
|
+
const sessionId = typeof sessionIdHeader === "string" ? sessionIdHeader : undefined;
|
|
142
|
+
const existing = sessionId ? sessions.get(sessionId) : undefined;
|
|
143
|
+
if (existing) existing.lastSeenMs = Date.now();
|
|
144
|
+
|
|
145
|
+
// A request naming a session that does not exist is answered, not granted a
|
|
146
|
+
// new one. Minting a transport for an unknown id created one that never
|
|
147
|
+
// entered `sessions`, so it could not be closed even by `closeSessions` on
|
|
148
|
+
// shutdown — a leak reachable by anyone sending a random header.
|
|
149
|
+
if (sessionId !== undefined && existing === undefined) {
|
|
150
|
+
res.writeHead(404, { "content-type": "application/json" });
|
|
151
|
+
res.end(JSON.stringify({ error: `Unknown session ${sessionId}` }));
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const transportPromise = existing ? Promise.resolve(existing.transport) : createSession();
|
|
156
|
+
transportPromise
|
|
157
|
+
.then((transport) => transport.handleRequest(req, res))
|
|
158
|
+
.catch((error) => {
|
|
159
|
+
if (!res.headersSent) {
|
|
160
|
+
res.writeHead(500, { "content-type": "application/json" });
|
|
161
|
+
}
|
|
162
|
+
res.end(JSON.stringify({ error: error instanceof Error ? error.message : String(error) }));
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
const port = options.port ?? options.defaultPort;
|
|
167
|
+
try {
|
|
168
|
+
await new Promise<void>((resolve, reject) => {
|
|
169
|
+
httpServer.once("error", reject);
|
|
170
|
+
httpServer.listen(port, host, () => resolve());
|
|
171
|
+
});
|
|
172
|
+
} catch (error) {
|
|
173
|
+
clearInterval(sweeper);
|
|
174
|
+
await closeSessions(sessions);
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const address = httpServer.address();
|
|
179
|
+
const actualPort = typeof address === "object" && address !== null ? address.port : port;
|
|
180
|
+
boundPort = actualPort;
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
url: `http://${host}:${actualPort}/mcp`,
|
|
184
|
+
port: actualPort,
|
|
185
|
+
close: async () => {
|
|
186
|
+
// Order matters and it used to be inverted. `httpServer.close()`'s
|
|
187
|
+
// callback fires only once every connection has ended, and the code that
|
|
188
|
+
// ends them — `closeSessions` — was *inside* that callback. An MCP client
|
|
189
|
+
// holds a keep-alive socket, so with anyone attached the callback never
|
|
190
|
+
// fired: `roomyx serve` did not exit on SIGTERM at all, the port stayed
|
|
191
|
+
// bound, and the only way out was SIGKILL. Measured with a control: with
|
|
192
|
+
// a client attached, still running after 12s; with none, clean.
|
|
193
|
+
//
|
|
194
|
+
// The management server used to skip closing sessions entirely, so its
|
|
195
|
+
// sessions outlived the process. One implementation, one behaviour.
|
|
196
|
+
clearInterval(sweeper);
|
|
197
|
+
await closeSessions(sessions);
|
|
198
|
+
|
|
199
|
+
await new Promise<void>((resolve, reject) => {
|
|
200
|
+
httpServer.close((error) => {
|
|
201
|
+
if (error) reject(error);
|
|
202
|
+
else resolve();
|
|
203
|
+
});
|
|
204
|
+
// A client that ignores the stream ending must not be able to pin the
|
|
205
|
+
// process open. Closing sessions first is the polite half; this is the
|
|
206
|
+
// half that means shutdown is bounded whatever the client does.
|
|
207
|
+
httpServer.closeAllConnections();
|
|
208
|
+
});
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function closeSessions(
|
|
214
|
+
sessions: Map<string, { transport: StreamableHTTPServerTransport; lastSeenMs: number }>,
|
|
215
|
+
): Promise<unknown[]> {
|
|
216
|
+
return Promise.all([...sessions.values()].map((entry) => entry.transport.close().catch(() => undefined)));
|
|
217
|
+
}
|
package/src/server/serve.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import { createServer } from "node:http";
|
|
2
|
-
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
3
1
|
import { createRoomMcpServer } from "./index";
|
|
4
2
|
import type { RoomMcpServerOptions } from "./index";
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { serveMcpOverHttp } from "./http-transport";
|
|
4
|
+
import type { McpHttpTransportHandle } from "./http-transport";
|
|
7
5
|
|
|
8
6
|
export interface ServeOptions extends RoomMcpServerOptions {
|
|
9
7
|
/** Defaults to 4319 (specification.md's default). 0 selects an ephemeral port. */
|
|
@@ -12,91 +10,20 @@ export interface ServeOptions extends RoomMcpServerOptions {
|
|
|
12
10
|
acknowledgeNonLoopback?: boolean;
|
|
13
11
|
}
|
|
14
12
|
|
|
15
|
-
export
|
|
16
|
-
url: string;
|
|
17
|
-
port: number;
|
|
18
|
-
close(): Promise<void>;
|
|
19
|
-
}
|
|
13
|
+
export type ServeHandle = McpHttpTransportHandle;
|
|
20
14
|
|
|
21
15
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* only ever completes ONE `initialize` handshake for its whole lifetime (an
|
|
26
|
-
* independent review reproduced this concretely: a second client, even a
|
|
27
|
-
* short-lived liveness check, gets "Server already initialized" and the
|
|
28
|
-
* transport is permanently unusable afterward). Each new session (a request
|
|
29
|
-
* with no `mcp-session-id` header) gets its own transport + `McpServer` pair,
|
|
30
|
-
* tracked by session id via `onsessioninitialized`/`onsessionclosed`;
|
|
31
|
-
* subsequent requests carrying that header are routed to the existing pair.
|
|
32
|
-
* Refuses non-loopback hosts unless explicitly acknowledged (mirrors `keryx
|
|
33
|
-
* serve`'s own convention — see docs/requirements/roomyx/decisions.md D-06).
|
|
16
|
+
* Serves one room log over the shared loopback HTTP transport. Everything about
|
|
17
|
+
* sessions, binding and shutdown lives in `./http-transport`; what belongs here
|
|
18
|
+
* is which server a session gets and which port it defaults to.
|
|
34
19
|
*/
|
|
35
20
|
export async function serve(logPath: string, options: ServeOptions): Promise<ServeHandle> {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const sessions = new Map<string, StreamableHTTPServerTransport>();
|
|
44
|
-
|
|
45
|
-
async function createSession(): Promise<StreamableHTTPServerTransport> {
|
|
46
|
-
const transport = new StreamableHTTPServerTransport({
|
|
47
|
-
sessionIdGenerator: () => crypto.randomUUID(),
|
|
48
|
-
onsessioninitialized: (sessionId) => {
|
|
49
|
-
sessions.set(sessionId, transport);
|
|
50
|
-
},
|
|
51
|
-
onsessionclosed: (sessionId) => {
|
|
52
|
-
sessions.delete(sessionId);
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
const mcpServer = createRoomMcpServer(logPath, { onOwnerCommand: options.onOwnerCommand });
|
|
56
|
-
await mcpServer.connect(transport);
|
|
57
|
-
return transport;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const httpServer = createServer((req, res) => {
|
|
61
|
-
const sessionIdHeader = req.headers["mcp-session-id"];
|
|
62
|
-
const sessionId = typeof sessionIdHeader === "string" ? sessionIdHeader : undefined;
|
|
63
|
-
const existing = sessionId ? sessions.get(sessionId) : undefined;
|
|
64
|
-
|
|
65
|
-
const transportPromise = existing ? Promise.resolve(existing) : createSession();
|
|
66
|
-
transportPromise
|
|
67
|
-
.then((transport) => transport.handleRequest(req, res))
|
|
68
|
-
.catch((error) => {
|
|
69
|
-
if (!res.headersSent) {
|
|
70
|
-
res.writeHead(500, { "content-type": "application/json" });
|
|
71
|
-
}
|
|
72
|
-
res.end(JSON.stringify({ error: error instanceof Error ? error.message : String(error) }));
|
|
73
|
-
});
|
|
21
|
+
return serveMcpOverHttp({
|
|
22
|
+
createMcpServer: () => createRoomMcpServer(logPath, { onOwnerCommand: options.onOwnerCommand }),
|
|
23
|
+
commandLabel: "roomyx serve",
|
|
24
|
+
defaultPort: 4319,
|
|
25
|
+
port: options.port,
|
|
26
|
+
host: options.host,
|
|
27
|
+
acknowledgeNonLoopback: options.acknowledgeNonLoopback,
|
|
74
28
|
});
|
|
75
|
-
|
|
76
|
-
const port = options.port ?? 4319;
|
|
77
|
-
try {
|
|
78
|
-
await new Promise<void>((resolve, reject) => {
|
|
79
|
-
httpServer.once("error", reject);
|
|
80
|
-
httpServer.listen(port, host, () => resolve());
|
|
81
|
-
});
|
|
82
|
-
} catch (error) {
|
|
83
|
-
await Promise.all([...sessions.values()].map((t) => t.close().catch(() => undefined)));
|
|
84
|
-
throw error;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const address = httpServer.address();
|
|
88
|
-
const actualPort = typeof address === "object" && address !== null ? address.port : port;
|
|
89
|
-
|
|
90
|
-
return {
|
|
91
|
-
url: `http://${host}:${actualPort}/mcp`,
|
|
92
|
-
port: actualPort,
|
|
93
|
-
close: () =>
|
|
94
|
-
new Promise<void>((resolve, reject) => {
|
|
95
|
-
httpServer.close(async (error) => {
|
|
96
|
-
await Promise.all([...sessions.values()].map((t) => t.close().catch(() => undefined)));
|
|
97
|
-
if (error) reject(error);
|
|
98
|
-
else resolve();
|
|
99
|
-
});
|
|
100
|
-
}),
|
|
101
|
-
};
|
|
102
29
|
}
|
package/src/cli/seed.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
import { appendFileSync, existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
-
import type { GoalContract, MessageEnvelope, RosterEntry } from "../log/types";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Manual-testing helper: initializes a room log with a state header, or
|
|
7
|
-
* appends one message to an existing log. Not part of the MCP server itself
|
|
8
|
-
* (D-01 in docs/requirements/roomyx/decisions.md — only the dispatcher
|
|
9
|
-
* writes the real log; this is a standalone fixture-seeding tool for local
|
|
10
|
-
* development, not a live dispatcher integration).
|
|
11
|
-
*
|
|
12
|
-
* Usage:
|
|
13
|
-
* bun src/cli/seed.ts init <path> --goal "<goal_statement>" --roster id1:Name1,id2:Name2
|
|
14
|
-
* bun src/cli/seed.ts append <path> --from <id> --body "<text>" [--kind pitch] [--in-reply-to N]
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
function parseFlags(args: string[]): Record<string, string> {
|
|
18
|
-
const flags: Record<string, string> = {};
|
|
19
|
-
for (let i = 0; i < args.length; i++) {
|
|
20
|
-
const arg = args[i];
|
|
21
|
-
if (arg?.startsWith("--")) {
|
|
22
|
-
flags[arg.slice(2)] = args[i + 1] ?? "";
|
|
23
|
-
i++;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return flags;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function nextSeq(path: string): number {
|
|
30
|
-
if (!existsSync(path)) return 1;
|
|
31
|
-
const lines = readFileSync(path, "utf8")
|
|
32
|
-
.split("\n")
|
|
33
|
-
.filter((l) => l.trim().length > 0);
|
|
34
|
-
const messageSeqs = lines
|
|
35
|
-
.slice(1)
|
|
36
|
-
.map((l) => (JSON.parse(l) as { seq: number }).seq);
|
|
37
|
-
return messageSeqs.length > 0 ? Math.max(...messageSeqs) + 1 : 1;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const [command, path, ...rest] = process.argv.slice(2);
|
|
41
|
-
const flags = parseFlags(rest);
|
|
42
|
-
|
|
43
|
-
if (command === "init" && path) {
|
|
44
|
-
const roster: RosterEntry[] = (flags.roster ?? "")
|
|
45
|
-
.split(",")
|
|
46
|
-
.filter(Boolean)
|
|
47
|
-
.map((pair) => {
|
|
48
|
-
const [id, name] = pair.split(":");
|
|
49
|
-
return { id: id ?? "", name: name ?? id ?? "" };
|
|
50
|
-
});
|
|
51
|
-
const goalContract: GoalContract = {
|
|
52
|
-
version: 1,
|
|
53
|
-
goal_statement: flags.goal ?? "Untitled room",
|
|
54
|
-
criteria: flags.criteria ?? "",
|
|
55
|
-
threshold: { fail_below: 60, pass_at_or_above: 80 },
|
|
56
|
-
};
|
|
57
|
-
writeFileSync(path, JSON.stringify({ type: "state", goal_contract: goalContract, roster }) + "\n");
|
|
58
|
-
console.log(`Initialized ${path} with ${roster.length} roster entries.`);
|
|
59
|
-
} else if (command === "append" && path) {
|
|
60
|
-
const message: MessageEnvelope & { type: "message" } = {
|
|
61
|
-
type: "message",
|
|
62
|
-
seq: nextSeq(path),
|
|
63
|
-
from: flags.from ?? "unknown",
|
|
64
|
-
body: flags.body ?? "",
|
|
65
|
-
...(flags.kind ? { kind: flags.kind as MessageEnvelope["kind"] } : {}),
|
|
66
|
-
...(flags["in-reply-to"] ? { in_reply_to: Number(flags["in-reply-to"]) } : {}),
|
|
67
|
-
};
|
|
68
|
-
appendFileSync(path, JSON.stringify(message) + "\n");
|
|
69
|
-
console.log(`Appended seq ${message.seq} from ${message.from} to ${path}.`);
|
|
70
|
-
} else {
|
|
71
|
-
console.error("Usage: bun src/cli/seed.ts init|append <path> [--flags...]");
|
|
72
|
-
process.exit(1);
|
|
73
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { BoxRenderable, ScrollBoxRenderable, TextRenderable } from "@opentui/core";
|
|
2
|
-
import type { RenderContext } from "@opentui/core";
|
|
3
|
-
import { createMessageRow } from "../components/message-row";
|
|
4
|
-
import type { AgentDetail } from "../../log/types";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Modal overlay for one participant's own messages (R4 / AC2 — shows only
|
|
8
|
-
* that participant's data, not the full transcript). Drawn over the chat
|
|
9
|
-
* view, not a screen replacement, matching the pattern observed in keryx's
|
|
10
|
-
* own mcp-inspector.ts (background stays visible, modal on top).
|
|
11
|
-
*/
|
|
12
|
-
export class AgentModal {
|
|
13
|
-
readonly node: BoxRenderable;
|
|
14
|
-
private readonly title: TextRenderable;
|
|
15
|
-
private readonly scroll: ScrollBoxRenderable;
|
|
16
|
-
private readonly ctx: RenderContext;
|
|
17
|
-
|
|
18
|
-
constructor(ctx: RenderContext, options: { width: number; height: number }) {
|
|
19
|
-
this.ctx = ctx;
|
|
20
|
-
this.node = new BoxRenderable(ctx, {
|
|
21
|
-
width: options.width,
|
|
22
|
-
height: options.height,
|
|
23
|
-
flexDirection: "column",
|
|
24
|
-
border: true,
|
|
25
|
-
visible: false,
|
|
26
|
-
position: "absolute",
|
|
27
|
-
top: 2,
|
|
28
|
-
left: 2,
|
|
29
|
-
zIndex: 10,
|
|
30
|
-
backgroundColor: "#000000",
|
|
31
|
-
});
|
|
32
|
-
this.title = new TextRenderable(ctx, { content: "", height: 1 });
|
|
33
|
-
this.node.add(this.title);
|
|
34
|
-
this.scroll = new ScrollBoxRenderable(ctx, { flexGrow: 1 });
|
|
35
|
-
this.node.add(this.scroll);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
show(detail: AgentDetail): void {
|
|
39
|
-
for (const child of this.scroll.getChildren()) this.scroll.remove(child);
|
|
40
|
-
if (!detail.found) {
|
|
41
|
-
this.title.content = "Agent not found";
|
|
42
|
-
this.node.visible = true;
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
this.title.content = `${detail.agent.name} — last seen at seq ${detail.lastSeenSeq}`;
|
|
46
|
-
for (const message of detail.messages) {
|
|
47
|
-
this.scroll.add(createMessageRow(this.ctx, message, detail.agent.name));
|
|
48
|
-
}
|
|
49
|
-
this.node.visible = true;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
hide(): void {
|
|
53
|
-
this.node.visible = false;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
isVisible(): boolean {
|
|
57
|
-
return this.node.visible;
|
|
58
|
-
}
|
|
59
|
-
}
|