fix: preserve full MCP schemas and relay server validation errors
This commit is contained in:
+1
-1
@@ -14,7 +14,7 @@ for await (const line of readline.createInterface({ input: process.stdin })) {
|
||||
send({ jsonrpc: '2.0', id: message.id, result: { tools: [
|
||||
{ name: 'echo', description: 'Owned fixture echo; annotations are not an authorization.', inputSchema: schema, annotations: { readOnlyHint: true } },
|
||||
{ name: 'hold', description: 'Owned side effect followed by lost result.', inputSchema: schema },
|
||||
{ name: 'unsupported', description: 'Unsupported schema must not become callable.', inputSchema: { type: 'object', properties: { n: { type: 'string', pattern: '^a$' } } } },
|
||||
{ name: 'unsupported', description: 'A non-object MCP input envelope must not become callable.', inputSchema: { type: 'array', items: { type: 'string' } } },
|
||||
] } });
|
||||
} else if (message.method === 'tools/call') {
|
||||
if (message.params.name === 'hold') { fs.writeFileSync(path.join(process.cwd(), 'owned-call-dispatched.txt'), JSON.stringify(message.params)); continue; }
|
||||
|
||||
Vendored
+54
@@ -0,0 +1,54 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import readline from 'node:readline';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
export function advancedSchema() {
|
||||
let nested = { const: 'leaf' };
|
||||
for (let index = 0; index < 24; index++) nested = { type: 'object', properties: { next: nested }, required: ['next'] };
|
||||
return { type: 'object', $schema: 'https://json-schema.org/draft/2020-12/schema',
|
||||
$defs: { choice: { oneOf: [ { type: 'object', properties: { kind: { const: 'text' }, value: { type: 'string', pattern: '^原文' } }, required: ['kind', 'value'], additionalProperties: false },
|
||||
{ type: 'object', properties: { kind: { const: 'number' }, value: { type: 'number', exclusiveMinimum: 0 } }, required: ['kind', 'value'] } ] },
|
||||
recursive: { anyOf: [{ type: 'null' }, { type: 'object', properties: { child: { $ref: '#/$defs/recursive' } } }] } },
|
||||
properties: { choice: { $ref: '#/$defs/choice' }, nested, rows: { type: 'array', items: { type: 'string' }, minItems: 2001 },
|
||||
untouched: { default: 'do-not-inject' }, recursive: { $ref: '#/$defs/recursive' }, extension: { $ref: 'https://example.invalid/owned-schema#remote' },
|
||||
booleanSchema: true, forbidden: false, tuple: { type: 'array', prefixItems: [{ type: 'string' }, { type: 'number' }], items: false },
|
||||
...Object.fromEntries(Array.from({ length: 80 }, (_, index) => ['property_' + index, { type: ['string', 'null'], description: '原声明注释'.repeat(70), enum: Array.from({ length: 80 }, (_, value) => 'value-' + value) }])) },
|
||||
required: ['choice', 'nested', 'rows'], allOf: [{ if: { properties: { flag: { const: true } }, required: ['flag'] }, then: { required: ['choice'] } }],
|
||||
patternProperties: { '^custom_': { type: 'integer' } }, additionalProperties: { type: ['string', 'boolean', 'number'] },
|
||||
'x-owned-vocabulary': { version: 7, note: 'Unknown server vocabulary is preserved, never executed by the client.' } };
|
||||
}
|
||||
export function advancedArguments() {
|
||||
let nested = 'leaf'; for (let index = 0; index < 24; index++) nested = { next: nested };
|
||||
return { choice: { kind: 'text', value: '原文保留,参数不转换' }, nested, rows: Array.from({ length: 2100 }, (_, index) => '原记录-' + index),
|
||||
recursive: { child: { child: null } }, custom_count: 3, property_79: 'value-79', tuple: ['original', 2] };
|
||||
}
|
||||
export function schemaFixtureTools() {
|
||||
return [ { name: 'advanced', description: '联合、引用、深层及大型原始 schema 的实际服务器校验', inputSchema: advancedSchema(), annotations: { readOnlyHint: true, title: '原服务端声明' }, 'x-owned-tool': { revision: 7 } },
|
||||
{ name: 'tool_error', description: '完整 CallToolResult.isError 原错', inputSchema: { type: 'object', properties: { fail: { const: true } }, required: ['fail'] } },
|
||||
{ name: 'rpc_error', description: 'JSON-RPC 原始错误,不自动重发', inputSchema: { type: 'object', properties: { detail: {} } } },
|
||||
{ name: 'invalid_root', description: '协议要求输入根为对象,此声明不符', inputSchema: { type: 'array', items: {} } },
|
||||
...Array.from({ length: 13 }, (_, index) => ({ name: 'extra_' + index, description: '跨旧12项目录门槛的自有工具 ' + index, inputSchema: { type: 'object', properties: { original: {} } } })) ];
|
||||
}
|
||||
|
||||
if (process.argv[1] && fs.realpathSync.native(process.argv[1]) === fs.realpathSync.native(fileURLToPath(import.meta.url))) {
|
||||
const send = value => process.stdout.write(JSON.stringify(value) + '\n');
|
||||
for await (const line of readline.createInterface({ input: process.stdin })) {
|
||||
let message; try { message = JSON.parse(line); } catch { continue; }
|
||||
if (message.id === undefined) continue;
|
||||
if (message.method === 'initialize') send({ jsonrpc: '2.0', id: message.id, result: { protocolVersion: '2025-03-26', capabilities: { tools: {} }, serverInfo: { name: 'owned-schema-server', version: '1.0' } } });
|
||||
else if (message.method === 'tools/list') {
|
||||
const tools = schemaFixtureTools();
|
||||
// A dependency can change independently of the registered entry.
|
||||
if (fs.existsSync(path.join(path.dirname(fileURLToPath(import.meta.url)), 'schema-drift.json'))) tools[0].inputSchema.properties.choice = { type: 'string' };
|
||||
send({ jsonrpc: '2.0', id: message.id, result: { tools } });
|
||||
} else if (message.method === 'tools/call') {
|
||||
fs.appendFileSync(path.join(process.cwd(), 'owned-schema-calls.jsonl'), JSON.stringify(message.params) + '\n');
|
||||
const { name, arguments: args } = message.params;
|
||||
if (name === 'rpc_error') send({ jsonrpc: '2.0', id: message.id, error: { code: -32602, message: '原服务器参数拒绝', data: { validator: 'owned-server', received: args, retryable: false } } });
|
||||
else if (name === 'tool_error' || args?.choice?.kind !== 'text' || typeof args?.choice?.value !== 'string' || !args.choice.value.startsWith('原文') || !Array.isArray(args.rows) || args.rows.length < 2001) {
|
||||
send({ jsonrpc: '2.0', id: message.id, result: { isError: true, content: [{ type: 'text', text: '原服务器校验失败:choice或rows不符合声明' }], _meta: { validator: 'owned-server', received: args } } });
|
||||
} else send({ jsonrpc: '2.0', id: message.id, result: { content: [{ type: 'text', text: '原服务器接受完整参数' }], _meta: { validator: 'owned-server', received: args, callId: message.id } } });
|
||||
} else send({ jsonrpc: '2.0', id: message.id, error: { code: -32601, message: 'Unknown owned method' } });
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,7 @@ test('MCP registration stays disabled until an explicit real stdio probe and sch
|
||||
assert.equal(report.initialize.serverInfo.version, '1.0'); assert.equal(report.tools.length, 3); assert.equal(job.cleanup.rootExited, true);
|
||||
assert.ok(report.tools.find(value => value.name === 'unsupported').unsupported);
|
||||
assert.deepEqual(report.trace.map(value => value.method), ['initialize', 'tools/list']); assert.equal(report.result, null);
|
||||
assert.throws(() => enable(f, ['unsupported']), /参数契约/); const enabled = enable(f); assert.equal(enabled.enabled, true);
|
||||
assert.throws(() => enable(f, ['unsupported']), /inputSchema.*MCP/); const enabled = enable(f); assert.equal(enabled.enabled, true);
|
||||
assert.equal(f.manager.catalog(f.project.id).entries.length, 1);
|
||||
assert.throws(() => f.manager.configure(f.project.id, { id: enabled.id, configurationRevision: 2, enabled: false, allowedTools: [] }), /配置已变化/);
|
||||
f.store.close(); const reopened = new Store(f.data); f.onCleanup(() => reopened.close());
|
||||
@@ -53,12 +53,16 @@ test('MCP probe holds the project operation lock until its owned process and rep
|
||||
assert.equal(saved.enabled, false);
|
||||
});
|
||||
|
||||
test('MCP validates the declared JSON schema subset and rejects unsupported schemas before external dispatch', () => {
|
||||
test('MCP keeps object envelopes and forwards schema semantics to the declaring server without coercion', () => {
|
||||
const schema = { type: 'object', properties: { count: { type: 'integer', minimum: 1, maximum: 3 }, name: { type: 'string', enum: ['alpha', 'beta'] } }, required: ['count'], additionalProperties: false };
|
||||
assert.equal(validateMcpArguments(schema, { count: 2, name: 'alpha' }), true);
|
||||
for (const value of [{}, { count: 4 }, { count: '2' }, { count: 2, other: true }, { count: 2, name: 'other' }]) assert.throws(() => validateMcpArguments(schema, value));
|
||||
assert.throws(() => checkMcpSchema({ type: 'object', $ref: '#/other' }), /schema/);
|
||||
assert.throws(() => checkMcpSchema({ type: 'object', properties: { x: { type: 'string', pattern: '.*' } } }), /schema/);
|
||||
for (const value of [{}, { count: 4 }, { count: '2' }, { count: 2, other: true }, { count: 2, name: 'other' }]) {
|
||||
const original = structuredClone(value); assert.equal(validateMcpArguments(schema, value), true); assert.deepEqual(value, original);
|
||||
}
|
||||
for (const value of [null, [], 'input']) assert.throws(() => validateMcpArguments(schema, value), /arguments.*JSON/);
|
||||
assert.equal(checkMcpSchema({ type: 'object', $ref: '#/other' }), true);
|
||||
assert.equal(checkMcpSchema({ type: 'object', properties: { x: { type: 'string', pattern: '.*' } } }), true);
|
||||
assert.throws(() => checkMcpSchema({ type: 'array' }), /inputSchema.*MCP/);
|
||||
});
|
||||
|
||||
test('actual model MCP call approves long schema-valid arguments, validates the sent catalog and stores one original result without leaking environment secrets', async t => {
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fixture, compatibleFixture, completion, call } from './helpers.mjs';
|
||||
import { Provider } from '../src/server/provider.mjs';
|
||||
import { Runtime } from '../src/server/runtime.mjs';
|
||||
import { Store } from '../src/server/store.mjs';
|
||||
import { checkMcpSchema, validateMcpArguments, mcpCatalogMessage } from '../src/core/tools/mcp-contract.mjs';
|
||||
import { verifyPlanEvidence } from '../src/core/context/request-plan.mjs';
|
||||
import { advancedSchema, advancedArguments, schemaFixtureTools } from './fixtures/schema-mcp-server.mjs';
|
||||
|
||||
async function setup(t) {
|
||||
const f = fixture(t, { modernDefaults: true }), provider = new Provider(); provider.setKey('owned-schema-key-canary');
|
||||
f.store.saveConfig({ approvalMode: 'full' });
|
||||
const runtime = new Runtime(f.store, provider); f.onCleanup(() => runtime.stopAll());
|
||||
const registry = runtime.mcpRegistry(), entry = path.join(f.root, 'schema-server.mjs'); fs.copyFileSync('tests/fixtures/schema-mcp-server.mjs', entry);
|
||||
let registration = registry.register(f.project.id, { name: 'Owned schema server', executable: process.execPath, entry, timeoutMs: 3000 });
|
||||
const probe = await registry.start(f.session, { id: registration.id, configurationRevision: registration.configurationRevision, confirm: true }).promise;
|
||||
assert.equal(probe.status, 'completed', probe.error?.message); registration = registry.find(f.project.id, registration.id);
|
||||
function enable(names) { registration = registry.configure(f.project.id, { id: registration.id, configurationRevision: registration.configurationRevision, enabled: true, allowedTools: names }); return registration; }
|
||||
function input(name, args) { return { registration_id: registration.id, configuration_revision: registration.configurationRevision, contract_sha256: registration.contract.sha256, tool: name, arguments: args }; }
|
||||
async function execute(name, args, beforeStart = () => {}) {
|
||||
const original = input(name, args);
|
||||
const server = await compatibleFixture(t, (_url, body, requests) => requests.length === 1 ? completion([call('owned-schema-call', 'call_registered_mcp', original)]) : completion([], 'Owned schema sequence finished.'));
|
||||
f.store.saveConfig({ mode: 'compatible', baseUrl: server.baseUrl, model: 'owned-schema-model' });
|
||||
await beforeStart(); runtime.start(f.session.id, '按已启用的原 schema 调用一次;原服务器错误不重试。'); await runtime.active.get(f.session.id).promise;
|
||||
const job = registry.jobs(f.session).find(value => value.operation === 'call'); assert.ok(job, JSON.stringify({ status: f.session.status, failure: f.session.lastFailure,
|
||||
toolMessages: f.session.providerMessages.filter(value => value.role === 'tool'), plans: [...f.store.core.context_plan.values()].map(value => ({ mcpCatalogEntries: value.mcpCatalogEntries, estimatedInputTokens: value.estimatedInputTokens })),
|
||||
config: { executionMode: f.store.config.executionMode, capacityMode: f.store.config.capacityMode, historyPolicy: f.store.config.historyPolicy } }));
|
||||
return { job, report: registry.report(f.session, job.id), requests: server.requests, original };
|
||||
}
|
||||
return { ...f, runtime, registry, entry, probe, enable, input, execute, registration: () => registration };
|
||||
}
|
||||
|
||||
test('MCP schema relay retains unions, recursive and external refs, boolean schemas, 80+ properties, deep schemas and server vocabulary without mutation or ref fetch', () => {
|
||||
const schema = advancedSchema(), args = advancedArguments(), before = structuredClone({ schema, args });
|
||||
assert.ok(Buffer.byteLength(JSON.stringify(schema)) > 16384); assert.ok(Buffer.byteLength(JSON.stringify(args)) > 16000);
|
||||
assert.ok(Object.keys(schema.properties).length > 64); assert.ok(args.rows.length > 2048);
|
||||
assert.equal(checkMcpSchema(schema), true); assert.equal(validateMcpArguments(schema, args, { executionMode: 'unrestricted' }), true);
|
||||
assert.deepEqual({ schema, args }, before);
|
||||
assert.equal(validateMcpArguments(schema, { choice: 'invalid-for-the-server' }, { executionMode: 'unrestricted' }), true);
|
||||
assert.throws(() => validateMcpArguments(schema, args, { executionMode: 'restricted' }), /手动限制/);
|
||||
for (const invalid of [null, [], { type: 'array' }, { type: 'object', properties: [] }, { type: 'object', required: [1] }]) assert.throws(() => checkMcpSchema(invalid), /inputSchema/);
|
||||
for (const invalid of [null, [], 'text']) assert.throws(() => validateMcpArguments(schema, invalid), /arguments/);
|
||||
});
|
||||
|
||||
test('actual MCP handshake, selection, frozen model catalog and tools/call preserve the full schema and 2100 deep argument records once, including restart and original SHA', async t => {
|
||||
const f = await setup(t), registration = f.enable(['advanced', ...Array.from({ length: 13 }, (_, index) => 'extra_' + index)]), contract = f.registry.contract(registration), originalSha = registration.contract.sha256;
|
||||
assert.equal(contract.schemaVersion, 2); assert.equal(contract.argumentValidation, 'server'); assert.deepEqual(contract.rawTools, schemaFixtureTools());
|
||||
assert.equal(f.registry.view(f.project.id).registrations[0].tools[0].unsupported, undefined);
|
||||
const args = advancedArguments(), { job, report, requests } = await f.execute('advanced', args);
|
||||
assert.equal(f.session.status, 'completed', JSON.stringify(f.session.error)); assert.equal(f.session.approvals.some(value => value.status === 'pending'), false);
|
||||
assert.ok(f.session.approvals.every(value => value.status === 'approved'));
|
||||
assert.equal(job.status, 'completed'); assert.equal(job.cleanup.rootExited, true); assert.equal(report.trace.filter(row => row.method === 'tools/call').length, 1);
|
||||
assert.deepEqual(report.rawTools, schemaFixtureTools()); assert.deepEqual(report.result._meta.received, args); assert.equal('untouched' in report.result._meta.received, false);
|
||||
const lines = fs.readFileSync(path.join(job.directory, 'owned-schema-calls.jsonl'), 'utf8').trim().split('\n'); assert.equal(lines.length, 1); assert.deepEqual(JSON.parse(lines[0]).arguments, args);
|
||||
assert.match(JSON.stringify(requests[0].input.messages), /参数按目录中的原始JSON Schema/);
|
||||
const plan = [...f.store.core.context_plan.values()].find(value => value.mcpCatalogArtifactId);
|
||||
verifyPlanEvidence(f.store, f.session, plan);
|
||||
const document = JSON.parse(f.store.artifactStore.read(f.store.core.artifact.get(plan.mcpCatalogArtifactId)));
|
||||
assert.equal(document.entries.length, 14); assert.deepEqual(document.entries[0].inputSchema, advancedSchema()); assert.equal(document.argumentValidation, 'server');
|
||||
await f.runtime.stopAll(); f.store.close(); const next = new Store(f.data); f.onCleanup(() => next.close()); const restored = new Runtime(next, new Provider()); f.onCleanup(() => restored.stopAll());
|
||||
const value = restored.mcpRegistry().find(f.project.id, registration.id); assert.equal(value.contract.sha256, originalSha);
|
||||
assert.deepEqual(restored.mcpRegistry().contract(value).rawTools, schemaFixtureTools()); assert.equal(restored.mcpRegistry().jobs(next.session(f.session.id)).length, 2);
|
||||
});
|
||||
|
||||
test('server CallToolResult validation errors are preserved once after dispatch, with no local coercion or inferred success', async t => {
|
||||
const f = await setup(t); f.enable(['advanced']); const args = { choice: { kind: 'text', value: 'invalid' }, rows: ['too short'] };
|
||||
const { job, report } = await f.execute('advanced', args);
|
||||
assert.equal(job.status, 'failed'); assert.equal(job.dispatched, true); assert.equal(job.requiresVerification, false);
|
||||
assert.equal(report.result.isError, true); assert.deepEqual(report.result._meta.received, args); assert.match(report.result.content[0].text, /原服务器校验失败/);
|
||||
assert.equal(report.trace.filter(row => row.method === 'tools/call').length, 1); assert.equal(f.registry.jobs(f.session).filter(value => value.operation === 'call').length, 1);
|
||||
});
|
||||
|
||||
test('JSON-RPC errors retain original code/data and an unknown side-effect boundary without replay across restart', async t => {
|
||||
const f = await setup(t); f.enable(['rpc_error']); const args = { detail: { original: '原参数', numericString: '007' } };
|
||||
const { job, report } = await f.execute('rpc_error', args);
|
||||
assert.equal(job.status, 'unknown'); assert.equal(job.requiresVerification, true); assert.equal(job.cleanup.rootExited, true);
|
||||
assert.deepEqual(report.error.rpcError, { code: -32602, message: '原服务器参数拒绝', data: { validator: 'owned-server', received: args, retryable: false } });
|
||||
assert.equal(report.trace.filter(row => row.method === 'tools/call').length, 1);
|
||||
await f.runtime.stopAll(); f.store.close(); const next = new Store(f.data); f.onCleanup(() => next.close()); assert.equal(next.core.mcp_job.get(job.id).status, 'unknown');
|
||||
assert.equal([...next.core.mcp_job.values()].filter(value => value.operation === 'call').length, 1);
|
||||
});
|
||||
|
||||
test('legacy unsupported labels are re-evaluated without rewriting the original contract or mistaking client classification for server drift', async t => {
|
||||
const f = await setup(t), value = f.registration(), previous = f.registry.contract(value);
|
||||
const legacy = { ...previous, schemaVersion: 1 }; delete legacy.argumentValidation; delete legacy.rawTools;
|
||||
legacy.tools = legacy.tools.map(tool => tool.name === 'advanced' ? { ...tool, unsupported: 'Historical 16KB/64-property subset rejection' } : tool);
|
||||
const artifact = f.store.artifactStore.write(f.session, f.store.core.run.get(f.session.runId), legacy, { kind: 'mcp-contract' }); f.store.core.artifact.set(artifact.id, artifact); f.store.save(f.session);
|
||||
const state = f.registry.state(); state.registrations[0].contract = { ...value.contract, artifactId: artifact.id, sha256: artifact.sha256 }; f.registry.save(state);
|
||||
const originalBytes = f.store.artifactStore.readBytes(artifact);
|
||||
assert.equal(f.registry.view(f.project.id).registrations[0].tools[0].unsupported, undefined); f.enable(['advanced']);
|
||||
const { job } = await f.execute('advanced', advancedArguments()); assert.equal(job.status, 'completed');
|
||||
assert.deepEqual(f.store.artifactStore.readBytes(artifact), originalBytes); assert.equal(f.registry.contract(f.registration()).tools[0].unsupported, 'Historical 16KB/64-property subset rejection');
|
||||
const oldDocument = { schemaVersion: 1, projectId: f.project.id, entries: [] }, reference = { id: 'original', sha256: 'original' };
|
||||
assert.equal(mcpCatalogMessage(reference, oldDocument).content.includes('参数按目录中的原始JSON Schema'), false);
|
||||
});
|
||||
|
||||
test('dynamic dependency schema drift still prevents tools/call before dispatch, and invalid MCP object envelopes remain unavailable', async t => {
|
||||
const f = await setup(t), registration = f.registration();
|
||||
assert.throws(() => f.enable(['invalid_root']), /inputSchema/);
|
||||
f.enable(['advanced']); fs.writeFileSync(path.join(f.root, 'schema-drift.json'), '{}');
|
||||
const { job, report } = await f.execute('advanced', advancedArguments());
|
||||
assert.equal(job.status, 'failed'); assert.equal(job.dispatched, false); assert.match(job.error.message, /schema已变化/);
|
||||
assert.equal(report.trace.some(row => row.method === 'tools/call'), false); assert.equal(registration.launch.entry.sha256, f.registry.find(f.project.id, registration.id).launch.entry.sha256);
|
||||
assert.equal(fs.existsSync(path.join(job.directory, 'owned-schema-calls.jsonl')), false);
|
||||
});
|
||||
Reference in New Issue
Block a user