File size: 1,565 Bytes
6c30253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import test from 'node:test';
import assert from 'node:assert/strict';
import { compactMcpResult, normalizeMcpTools } from '../src/tools/mcp-client.js';
import { prepareToolCall } from '../src/tools/tool-registry.js';

test('normalizes compatible MCP tools for the existing tool loop', () => {
  const [tool] = normalizeMcpTools([{
    name: 'search_docs',
    description: 'Search documentation.',
    inputSchema: {
      type: 'object',
      properties: { query: { type: 'string' } },
      required: ['query'],
      additionalProperties: false,
    },
  }]);
  assert.equal(tool.source, 'mcp');
  assert.equal(tool.enabled, false);
  tool.enabled = true;
  const prepared = prepareToolCall({ name: 'search_docs', arguments: { query: 'WebGPU' }, positional: [] }, [tool]);
  assert.deepEqual(prepared.args, { query: 'WebGPU' });
});

test('skips MCP tools with unsupported names or non-object schemas', () => {
  const tools = normalizeMcpTools([
    { name: 'bad-name', inputSchema: { type: 'object', properties: {} } },
    { name: 'primitive', inputSchema: { type: 'string' } },
    { name: 'valid_tool', inputSchema: { type: 'object', properties: {} } },
  ]);
  assert.deepEqual(tools.map(tool => tool.name), ['valid_tool']);
});

test('compacts large MCP text results before returning them to the model', () => {
  const result = compactMcpResult({ content: [{ type: 'text', text: 'x'.repeat(18000) }] });
  assert.equal(result.truncated, true);
  assert.equal(result.content[0].text.length, 4000);
  assert.ok(JSON.stringify(result).length < 4200);
});