expose the clide command surface over MCP (T-225)
External MCP clients (Cursor, Windsurf, Copilot) can now drive clide. The MCP server's tools/list is generated from the co-registered command+schema registry (D-74) that already feeds the CLI and palette — the full mcp__clide__* namespace with no hand-maintained second surface (D-86). Each command's CommandSchema maps to a JSON-Schema inputSchema; tools/call routes mcp__clide__<cmd> to dispatcher.dispatch and renders the IpcResponse as MCP content (data as JSON, errors with isError). register() gains a mcpExpose flag (default true); pane.tail opts out as a poor request/response fit. tail/events are server-intercepted so they're naturally absent. The two /ide stubs (getDiagnostics, executeCode) are left as stubs — making them real (analyzer hook, Jupyter eval) is out of scope per the ticket. Transport unchanged (SSE, D-73). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -89,6 +89,79 @@ void main() {
|
||||
expect(echo.containsKey('args'), isFalse);
|
||||
});
|
||||
|
||||
test('mcpTools generates the tool surface from the registry (T-225)', () async {
|
||||
final d = DaemonDispatcher();
|
||||
d.register('echo', (req) async => IpcResponse.ok(id: req.id, data: const {}));
|
||||
d.register(
|
||||
'pane.resize',
|
||||
(req) async => IpcResponse.ok(id: req.id, data: const {}),
|
||||
schema: const CommandSchema(
|
||||
positional: ['id', 'cols'],
|
||||
args: {
|
||||
'id': ArgSpec(required: true),
|
||||
'cols': ArgSpec(type: ArgType.number, min: 1),
|
||||
},
|
||||
),
|
||||
);
|
||||
d.register('pane.tail', (req) async => IpcResponse.ok(id: req.id, data: const {}), mcpExpose: false);
|
||||
d.register(
|
||||
'files.read',
|
||||
(req) async => IpcResponse.ok(id: req.id, data: const {}),
|
||||
schema: const CommandSchema(
|
||||
positional: ['path'],
|
||||
args: {
|
||||
'path': ArgSpec(pattern: null, allowed: {'a', 'b'}),
|
||||
'recursive': ArgSpec(type: ArgType.boolean),
|
||||
'globs': ArgSpec(type: ArgType.stringList, maxItems: 5),
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
// Non-const so a RegExp pattern can be supplied (covers the string
|
||||
// `pattern` mapping).
|
||||
d.register(
|
||||
'git.checkout',
|
||||
(req) async => IpcResponse.ok(id: req.id, data: const {}),
|
||||
schema: CommandSchema(positional: const ['ref'], args: {'ref': ArgSpec(pattern: RegExp(r'^\w+$'))}),
|
||||
);
|
||||
|
||||
final tools = d.mcpTools();
|
||||
final byName = {for (final t in tools) t['name'] as String: t};
|
||||
|
||||
// Tools are prefixed; built-ins + registered commands are present.
|
||||
expect(byName.keys, containsAll(['mcp__clide__ping', 'mcp__clide__echo', 'mcp__clide__pane.resize']));
|
||||
// The opt-out command is withheld.
|
||||
expect(byName.containsKey('mcp__clide__pane.tail'), isFalse);
|
||||
|
||||
// Schema → JSON-Schema inputSchema.
|
||||
final resize = byName['mcp__clide__pane.resize']!;
|
||||
expect(resize['description'], 'pane: resize');
|
||||
final input = resize['inputSchema'] as Map<String, Object?>;
|
||||
expect(input['type'], 'object');
|
||||
final props = input['properties'] as Map<String, Object?>;
|
||||
expect((props['cols'] as Map)['type'], 'number');
|
||||
expect((props['cols'] as Map)['minimum'], 1);
|
||||
expect(input['required'], ['id']);
|
||||
|
||||
// A schema-less command gets an empty object input with no required.
|
||||
final echo = byName['mcp__clide__echo']!;
|
||||
final echoInput = echo['inputSchema'] as Map<String, Object?>;
|
||||
expect(echoInput['properties'], isEmpty);
|
||||
expect(echoInput.containsKey('required'), isFalse);
|
||||
|
||||
// Each ArgType maps to its JSON-Schema shape.
|
||||
final readProps = (byName['mcp__clide__files.read']!['inputSchema'] as Map)['properties'] as Map;
|
||||
expect((readProps['path'] as Map)['type'], 'string');
|
||||
expect((readProps['path'] as Map)['enum'], ['a', 'b']);
|
||||
expect((readProps['recursive'] as Map)['type'], 'boolean');
|
||||
expect((readProps['globs'] as Map)['type'], 'array');
|
||||
expect(((readProps['globs'] as Map)['items'] as Map)['type'], 'string');
|
||||
expect((readProps['globs'] as Map)['maxItems'], 5);
|
||||
|
||||
final refProps = (byName['mcp__clide__git.checkout']!['inputSchema'] as Map)['properties'] as Map;
|
||||
expect((refProps['ref'] as Map)['pattern'], r'^\w+$');
|
||||
});
|
||||
|
||||
test('clear removes user handlers but keeps the built-ins', () async {
|
||||
final d = DaemonDispatcher();
|
||||
d.register('extra', (req) async => IpcResponse.ok(id: req.id, data: const {}));
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:clide/clide.dart';
|
||||
import 'package:clide/kernel/src/log.dart';
|
||||
import 'package:clide/src/ipc/mcp_server.dart';
|
||||
import 'package:test/test.dart';
|
||||
@@ -202,4 +203,139 @@ void main() {
|
||||
expect(lines, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('McpServer (T-225) clide tool surface', () {
|
||||
late Directory disc;
|
||||
late McpServer srv;
|
||||
|
||||
setUp(() async {
|
||||
disc = await Directory.systemTemp.createTemp('clide-mcp-clide-');
|
||||
final dispatcher = DaemonDispatcher();
|
||||
dispatcher.register('echo', (req) async => IpcResponse.ok(id: req.id, data: {'echo': req.args['text']}));
|
||||
// A poor MCP fit — withheld from the tool surface (D-86).
|
||||
dispatcher.register('pane.tail', (req) async => IpcResponse.ok(id: req.id, data: const {}), mcpExpose: false);
|
||||
srv = McpServer(
|
||||
workspaceRoot: '/x',
|
||||
log: _silent(),
|
||||
discoveryDirOverride: disc.path,
|
||||
dispatcher: dispatcher,
|
||||
);
|
||||
await srv.start();
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
try {
|
||||
await srv.stop();
|
||||
} catch (_) {}
|
||||
if (disc.existsSync()) disc.deleteSync(recursive: true);
|
||||
});
|
||||
|
||||
Future<(String, Stream<String>)> connect() async {
|
||||
final client = HttpClient();
|
||||
addTearDown(client.close);
|
||||
final resp = await (await client.getUrl(Uri.parse('http://127.0.0.1:${srv.port}/sse'))).close();
|
||||
final dataLines =
|
||||
resp.transform(utf8.decoder).transform(const LineSplitter()).where((l) => l.startsWith('data: ')).map((l) => l.substring(6)).asBroadcastStream();
|
||||
final endpoint = Completer<String>();
|
||||
final sub = dataLines.listen((d) {
|
||||
final m = RegExp(r'sessionId=([\w-]+)').firstMatch(d);
|
||||
if (m != null && !endpoint.isCompleted) endpoint.complete(m.group(1)!);
|
||||
});
|
||||
final id = await endpoint.future.timeout(const Duration(seconds: 2));
|
||||
await sub.cancel();
|
||||
return (id, dataLines);
|
||||
}
|
||||
|
||||
Future<void> post(String sid, Map<String, Object?> body) async {
|
||||
final client = HttpClient();
|
||||
addTearDown(client.close);
|
||||
final req = await client.postUrl(Uri.parse('http://127.0.0.1:${srv.port}/messages?sessionId=$sid'));
|
||||
req.headers.contentType = ContentType.json;
|
||||
req.write(jsonEncode(body));
|
||||
final resp = await req.close();
|
||||
expect(resp.statusCode, HttpStatus.accepted);
|
||||
}
|
||||
|
||||
test('tools/list adds generated mcp__clide__ tools alongside the /ide pair', () async {
|
||||
final (sid, events) = await connect();
|
||||
final replyFuture = events.firstWhere((s) => s.contains('"id":10'));
|
||||
await post(sid, {'jsonrpc': '2.0', 'id': 10, 'method': 'tools/list'});
|
||||
final reply = jsonDecode(await replyFuture.timeout(const Duration(seconds: 2))) as Map<String, Object?>;
|
||||
final names = ((reply['result'] as Map)['tools'] as List).map((t) => (t as Map)['name']).toSet();
|
||||
expect(names, containsAll(['mcp__ide__getDiagnostics', 'mcp__clide__echo', 'mcp__clide__ping']));
|
||||
// The opt-out command is withheld.
|
||||
expect(names.contains('mcp__clide__pane.tail'), isFalse);
|
||||
});
|
||||
|
||||
test('tools/call routes mcp__clide__ tools to the dispatcher', () async {
|
||||
final (sid, events) = await connect();
|
||||
final replyFuture = events.firstWhere((s) => s.contains('"id":11'));
|
||||
await post(sid, {
|
||||
'jsonrpc': '2.0',
|
||||
'id': 11,
|
||||
'method': 'tools/call',
|
||||
'params': {
|
||||
'name': 'mcp__clide__echo',
|
||||
'arguments': {'text': 'hi'},
|
||||
},
|
||||
});
|
||||
final reply = jsonDecode(await replyFuture.timeout(const Duration(seconds: 2))) as Map<String, Object?>;
|
||||
final content = ((reply['result'] as Map)['content'] as List).cast<Map<String, Object?>>();
|
||||
expect(jsonDecode(content.first['text'] as String), {'echo': 'hi'});
|
||||
});
|
||||
|
||||
test('a failing clide tool surfaces isError with the error message', () async {
|
||||
// An unknown command → dispatcher returns a not-found error.
|
||||
final (sid, events) = await connect();
|
||||
final replyFuture = events.firstWhere((s) => s.contains('"id":12'));
|
||||
await post(sid, {
|
||||
'jsonrpc': '2.0',
|
||||
'id': 12,
|
||||
'method': 'tools/call',
|
||||
'params': {'name': 'mcp__clide__nope.verb', 'arguments': const {}},
|
||||
});
|
||||
final reply = jsonDecode(await replyFuture.timeout(const Duration(seconds: 2))) as Map<String, Object?>;
|
||||
final result = reply['result'] as Map<String, Object?>;
|
||||
expect(result['isError'], isTrue);
|
||||
expect((result['content'] as List).first['text'], contains('unknown command'));
|
||||
});
|
||||
});
|
||||
|
||||
test('calling a clide tool with no dispatcher wired surfaces isError', () async {
|
||||
final disc = await Directory.systemTemp.createTemp('clide-mcp-nodisp-');
|
||||
final srv = McpServer(workspaceRoot: '/x', log: _silent(), discoveryDirOverride: disc.path);
|
||||
await srv.start();
|
||||
addTearDown(() async {
|
||||
await srv.stop();
|
||||
if (disc.existsSync()) disc.deleteSync(recursive: true);
|
||||
});
|
||||
final client = HttpClient();
|
||||
addTearDown(client.close);
|
||||
final resp = await (await client.getUrl(Uri.parse('http://127.0.0.1:${srv.port}/sse'))).close();
|
||||
final data = resp
|
||||
.transform(utf8.decoder)
|
||||
.transform(const LineSplitter())
|
||||
.where((l) => l.startsWith('data: '))
|
||||
.map((l) => l.substring(6))
|
||||
.asBroadcastStream();
|
||||
final ep = Completer<String>();
|
||||
final sub = data.listen((d) {
|
||||
final m = RegExp(r'sessionId=([\w-]+)').firstMatch(d);
|
||||
if (m != null && !ep.isCompleted) ep.complete(m.group(1)!);
|
||||
});
|
||||
final sid = await ep.future.timeout(const Duration(seconds: 2));
|
||||
await sub.cancel();
|
||||
final replyFuture = data.firstWhere((s) => s.contains('"id":13'));
|
||||
final post = await client.postUrl(Uri.parse('http://127.0.0.1:${srv.port}/messages?sessionId=$sid'));
|
||||
post.headers.contentType = ContentType.json;
|
||||
post.write(jsonEncode({
|
||||
'jsonrpc': '2.0',
|
||||
'id': 13,
|
||||
'method': 'tools/call',
|
||||
'params': {'name': 'mcp__clide__echo', 'arguments': const {}},
|
||||
}));
|
||||
await post.close();
|
||||
final reply = jsonDecode(await replyFuture.timeout(const Duration(seconds: 2))) as Map<String, Object?>;
|
||||
expect((reply['result'] as Map)['isError'], isTrue);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user