Files
Agent-Workbench/tests/mcp-schema.test.mjs
2026-11-07 10:47:31 +08:00

109 lines
11 KiB
JavaScript

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);
});