Skip to content

Commit 72a9283

Browse files
authored
add --exclude-tool option to exclude tools by name (#253)
Closes #251 cc @DanTup
1 parent 0dbbd8b commit 72a9283

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

pkgs/dart_mcp_server/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Change the widget tree to the full version instead of the summary. The summary
1212
tends to hide nested text widgets which makes it difficult to find widgets
1313
based on their text values.
14+
* Add an `--exclude-tool` command line flag to exclude tools by name.
1415

1516
# 0.1.0 (Dart SDK 3.9.0)
1617

pkgs/dart_mcp_server/lib/src/arg_parser.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,18 @@ ArgParser createArgParser({
4747
help:
4848
'Path to a file to log all MPC protocol traffic to. File will be '
4949
'overwritten if it exists.',
50+
)
51+
..addMultiOption(
52+
excludeToolOption,
53+
help: 'The names of tools to exclude from this run of the server.',
5054
);
5155

5256
if (includeHelp) parser.addFlag(helpFlag, abbr: 'h', help: 'Show usage text');
5357
return parser;
5458
}
5559

5660
const dartSdkOption = 'dart-sdk';
61+
const excludeToolOption = 'exclude-tool';
5762
const flutterSdkOption = 'flutter-sdk';
5863
const forceRootsFallbackFlag = 'force-roots-fallback';
5964
const helpFlag = 'help';

pkgs/dart_mcp_server/lib/src/server.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ final class DartMCPServer extends MCPServer
4848
ProcessManagerSupport,
4949
FileSystemSupport,
5050
SdkSupport {
51+
/// A list of tool names to exclude from this version of the server.
52+
///
53+
/// Used in [registerTool] to skip registering these tools.
54+
final List<String> excludedTools;
55+
5156
DartMCPServer(
5257
super.channel, {
5358
required this.sdk,
5459
this.analytics,
60+
this.excludedTools = const [],
5561
@visibleForTesting this.processManager = const LocalProcessManager(),
5662
@visibleForTesting this.fileSystem = const LocalFileSystem(),
5763
this.forceRootsFallback = false,
@@ -96,6 +102,7 @@ final class DartMCPServer extends MCPServer
96102
() {
97103
server = DartMCPServer(
98104
stdioChannel(input: io.stdin, output: io.stdout),
105+
excludedTools: parsedArgs.multiOption(excludeToolOption),
99106
forceRootsFallback: parsedArgs.flag(forceRootsFallbackFlag),
100107
sdk: Sdk.find(
101108
dartSdkPath: dartSdkPath,
@@ -167,6 +174,9 @@ final class DartMCPServer extends MCPServer
167174
FutureOr<CallToolResult> Function(CallToolRequest) impl, {
168175
bool validateArguments = true,
169176
}) {
177+
// Check manually excluded tools and skip them.
178+
if (excludedTools.contains(tool.name)) return;
179+
170180
// For type promotion.
171181
final analytics = this.analytics;
172182

pkgs/dart_mcp_server/test/dart_tooling_mcp_server_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import 'dart:async';
66
import 'dart:io';
77

88
import 'package:dart_mcp/server.dart';
9+
import 'package:dart_mcp_server/src/arg_parser.dart';
10+
import 'package:dart_mcp_server/src/mixins/analyzer.dart';
11+
import 'package:dart_mcp_server/src/mixins/dtd.dart';
912
import 'package:dart_mcp_server/src/server.dart';
1013
import 'package:dart_mcp_server/src/utils/analytics.dart';
1114
import 'package:test/test.dart';
@@ -256,6 +259,28 @@ void main() {
256259
await doWithRetries(() => File(logDescriptor.io.path).delete());
257260
});
258261
});
262+
263+
test('Tools can be disabled with --exclude-tool', () async {
264+
final testHarness = await TestHarness.start(
265+
cliArgs: [
266+
'--$excludeToolOption',
267+
DartAnalyzerSupport.analyzeFilesTool.name,
268+
'--$excludeToolOption',
269+
DartToolingDaemonSupport.connectTool.name,
270+
],
271+
);
272+
final connection = testHarness.serverConnectionPair.serverConnection;
273+
final tools = (await connection.listTools()).tools;
274+
expect(
275+
tools,
276+
isNot(contains(equals(DartAnalyzerSupport.analyzeFilesTool))),
277+
);
278+
expect(
279+
tools,
280+
isNot(contains(equals(DartToolingDaemonSupport.connectTool))),
281+
);
282+
expect(tools, contains(equals(DartAnalyzerSupport.hoverTool)));
283+
});
259284
}
260285

261286
/// Performs [action] up to [maxRetries] times, backing off an extra 50ms

0 commit comments

Comments
 (0)