feat(tools): implements optional tool exposure (#68)

Allows administrators to specify either a whitelist or a blacklist for determining which tools are desired. This work introduces new command-line arguments `--enabled-tools` and `--disabled-tools`, which cannot be used together. Likewise, this work also considers environment variables `BRAVE_MCP_ENABLED_TOOLS` and `BRAVE_MCP_DISABLED_TOOLS` as another means of communicating desired or undesired tools. See https://github.com/brave/brave-search-mcp-server/issues/62 for more on the motivation behind this feature.
This commit is contained in:
Sampson authored and GitHub committed 2025-09-10 09:03:58 -05:00
1 parent 0bca1a42b3
commit 9f87c8bb93
4 files changed
+50 -1

No files matched your search

+40
View File
@@ -2,6 +2,8 @@ import { LoggingLevel, LoggingLevelSchema } from '@modelcontextprotocol/sdk/type
import { Command } from 'commander';
import dotenv from 'dotenv';
import { z } from 'zod';
import tools from './tools/index.js';
dotenv.config({ debug: false, quiet: true });
// Config schema for Smithery.ai
@@ -22,6 +24,8 @@ type Configuration = {
host: string;
braveApiKey: string;
loggingLevel: LoggingLevel;
enabledTools: string[];
disabledTools: string[];
};
const state: Configuration & { ready: boolean } = {
@@ -31,8 +35,18 @@ const state: Configuration & { ready: boolean } = {
braveApiKey: process.env.BRAVE_API_KEY ?? '',
loggingLevel: 'info',
ready: false,
enabledTools: [],
disabledTools: [],
};
const toolNames = Object.values(tools).map((tool) => tool.name);
export function isToolPermittedByUser(toolName: string): boolean {
return state.enabledTools.length > 0
? state.enabledTools.includes(toolName)
: state.disabledTools.includes(toolName) === false;
}
export function getOptions(): Configuration | false {
const program = new Command()
.option('--brave-api-key <string>', 'Brave API key', process.env.BRAVE_API_KEY ?? '')
@@ -42,6 +56,16 @@ export function getOptions(): Configuration | false {
'transport type',
process.env.BRAVE_MCP_TRANSPORT ?? 'stdio'
)
.option(
'--enabled-tools <names...>',
'tools to enable',
process.env.BRAVE_MCP_ENABLED_TOOLS?.split(' ') ?? []
)
.option(
'--disabled-tools <names...>',
'tools to disable',
process.env.BRAVE_MCP_DISABLED_TOOLS?.split(' ') ?? []
)
.option(
'--port <number>',
'desired port for HTTP transport',
@@ -57,6 +81,20 @@ export function getOptions(): Configuration | false {
const options = program.opts();
// Validate tool inclusion configuration
const { enabledTools, disabledTools } = options;
if (enabledTools.length > 0 && disabledTools.length > 0) {
console.error('Error: --enabled-tools and --disabled-tools cannot be used together');
return false;
}
if ([...enabledTools, ...disabledTools].some((t) => !toolNames.includes(t))) {
console.error(`Invalid tool name used. Must be one of: ${toolNames.join(', ')}`);
return false;
}
// Validate all other options
if (!['stdio', 'http'].includes(options.transport)) {
console.error(
`Invalid --transport value: '${options.transport}'. Must be one of: stdio, http.`
@@ -98,6 +136,8 @@ export function getOptions(): Configuration | false {
state.port = options.port;
state.host = options.host;
state.loggingLevel = options.loggingLevel;
state.enabledTools = options.enabledTools;
state.disabledTools = options.disabledTools;
state.ready = true;
return options as Configuration;
+3 -1
View File
@@ -2,7 +2,7 @@ import tools from './tools/index.js';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { SetLevelRequestSchema } from '@modelcontextprotocol/sdk/types.js';
import pkg from '../package.json' with { type: 'json' };
import { isToolPermittedByUser } from './config.js';
import { type SmitheryConfig, setOptions } from './config.js';
export { configSchema } from './config.js';
@@ -39,6 +39,8 @@ export default function createMcpServer(options?: CreateMcpServerOptions): McpSe
}
for (const tool of Object.values(tools)) {
// The user may have enabled/disabled this tool at runtime
if (!isToolPermittedByUser(tool.name)) continue;
tool.register(mcpServer);
}