Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions harness/ruview/.harness/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"src/hosts/index.js": "ab276c41ab722bcdf72c2d1649cecbb760ae05c41c1372aae4c2447aa7c11539",
"src/kernel.js": "48dfd218a747dd158f82ae108e7e8ed89a82cd68649214d23266112dc9033f5d",
"src/mcp-http.js": "b72e021ae00d01831ef7184db824f5e85a2b8747e1d8e994b6a1cb1cb4d08945",
"src/mcp-server.js": "5eb8c3431768932c3a85476df032d0608fc7a732ede3d6bdcc8467ca47552666",
"src/mcp-server.js": "0d907a8763b1c25a5dc73c01bd4ed5d0fba0cd4af9dcc3354e862cea97d195ac",
"src/policy.js": "baad5eb8c3eb2e416e0a71b342ad850d7c8413d7d976571ebf596937f69da8e2",
"src/process-runner.js": "49533b038044dfb8bc76ed01c030d06a9856ead0836157fb693e2a7d40f786d6",
"src/redact.js": "ebf1afff46341078706b0401838c53db043603586e280d51ece5cf1feba35189",
Expand All @@ -75,7 +75,7 @@
"src/training.js": "bd8a6c12eba4f9d2f8990fa9692cd04c743ea247f9b13e10f8717ceefcbd09ac",
"src/ui/console-widget.js": "0d2f707d57b5ec54968dee1dc80f31c7db9152dcadfc48700c18379dabb55462"
},
"filesDigest": "edc6be28115ad21f18954066e989a6b4aaae2137b2f4e6d6571312beef610288",
"filesDigest": "13c9cfe56f226fedaa4cf5f692e0c338de74c072408cbe03870b31caf6981050",
"brainDigest": "c0fb7b079ded157059b91601361429944697dae3cc42abc00dfe1a680986b0f4",
"gateFingerprint": "6e53c784eee38310188948fc75fb49e6b4ebc04e247d01b903fa8c8a92d67bdd",
"developmentPins": {
Expand Down
2 changes: 1 addition & 1 deletion harness/ruview/.harness/manifest.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0517e22827b8acc7d8550e1e560e4f13ca79397977c52c30cf23770dead2327a manifest.json
c9bb5d659e413dcbfebb52c982b9954cc248ff27828d2cbe71d0b89bca300250 manifest.json
34 changes: 30 additions & 4 deletions harness/ruview/src/mcp-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// JSON text block, and the UI tools point at a self-contained `ui://` widget
// for MCP Apps hosts and ChatGPT.

import { createInterface } from 'node:readline';
import { readFileSync } from 'node:fs';
import { listTools, resolveToolName, runTool } from './tools.js';
import { CONSOLE_HTML, CONSOLE_RESOURCE, CONSOLE_RESOURCE_META } from './ui/console-widget.js';
Expand Down Expand Up @@ -150,20 +149,47 @@ export function parseGrants(env = process.env) {
/** stdio transport. `handler` lets an embedding package (the `ruview` umbrella) serve a merged tool set. */
export function startMcpServer({ handler = handleRpc, label = `${listTools().length} tools` } = {}) {
log(`starting v${SERVER_INFO.version} (protocol ${SUPPORTED_PROTOCOLS[0]}, ${label}, stdio)`);
const rl = createInterface({ input: process.stdin, crlfDelay: Infinity });
const { dispatch, idle } = createDispatcher({ source: 'mcp', transport: 'stdio', grants: parseGrants() }, handler);
const send = (res) => { if (res) process.stdout.write(JSON.stringify(res) + '\n'); };

rl.on('line', (line) => {
const acceptLine = (line) => {
if (Buffer.byteLength(line, 'utf8') > MAX_REQUEST_BYTES) { log('oversized JSON-RPC line dropped'); return; }
const s = line.trim();
if (!s) return;
let msg;
try { msg = JSON.parse(s); } catch { log('bad JSON line dropped'); return; }
dispatch(msg).then(send);
};

// Bound bytes during assembly, rather than after readline has buffered a
// complete string. Preserve UTF-8 across chunks and discard only this line.
let chunks = [];
let bufferedBytes = 0;
let discarding = false;
process.stdin.on('data', (value) => {
const data = Buffer.isBuffer(value) ? value : Buffer.from(value);
let offset = 0;
while (offset < data.length) {
const newline = data.indexOf(0x0a, offset);
const end = newline === -1 ? data.length : newline;
const segment = data.subarray(offset, end);
if (!discarding) {
if (bufferedBytes + segment.length > MAX_REQUEST_BYTES) {
log('oversized JSON-RPC line dropped');
chunks = []; bufferedBytes = 0; discarding = true;
} else if(segment.length) {
chunks.push(Buffer.from(segment)); bufferedBytes += segment.length;
}
}
if (newline === -1) break;
if (!discarding) acceptLine(Buffer.concat(chunks, bufferedBytes).toString('utf8'));
chunks = []; bufferedBytes = 0; discarding = false;
offset = newline + 1;
}
});

rl.on('close', () => {
process.stdin.on('end', () => {
if (!discarding && bufferedBytes) acceptLine(Buffer.concat(chunks, bufferedBytes).toString('utf8'));
// Wait for any queued/in-flight tool call to settle (its response written)
// before exiting — fire-and-forget used to race this and drop the response.
idle().then(() => setImmediate(() => {
Expand Down
26 changes: 26 additions & 0 deletions harness/ruview/test/mcp-framing.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { once } from 'node:events';

test('MCP drops an oversized unfinished line before newline and accepts the next fragmented request', async () => {
const child = spawn(process.execPath, ['bin/cli.js', 'mcp', 'start'], {stdio:['pipe','pipe','pipe']});
let stderr = '', stdout = '';
child.stderr.on('data', data => stderr += data);
child.stdout.on('data', data => stdout += data);
try {
const chunk = Buffer.alloc(64 * 1024, 0x78);
for(let i=0;i<6;i++) if(!child.stdin.write(chunk)) await once(child.stdin, 'drain');
const deadline = Date.now()+1000;
while(!stderr.includes('oversized JSON-RPC line dropped') && Date.now()<deadline) await new Promise(resolve => setTimeout(resolve, 10));
assert.match(stderr, /oversized JSON-RPC line dropped/, 'discard the unfinished line as soon as the bound is exceeded');
child.stdin.write('\n');
const request = Buffer.from(JSON.stringify({jsonrpc:'2.0',id:'unicode-✓',method:'ping'})+'\n');
const split = request.indexOf(Buffer.from('✓'))+1;
child.stdin.write(request.subarray(0, split));
child.stdin.end(request.subarray(split));
const code = await Promise.race([once(child,'close').then(([code])=>code),new Promise((_,reject)=>setTimeout(()=>reject(new Error('server did not finish')),3000))]);
assert.equal(code,0);
assert.deepEqual(JSON.parse(stdout.trim()), {jsonrpc:'2.0',id:'unicode-✓',result:{}});
} finally { child.kill('SIGKILL'); }
});