diff --git a/README.md b/README.md index ccf0638..e070a1f 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,8 @@ The server supports the following environment variables: - `BRAVE_MCP_PORT`: HTTP server port (default: 8080) - `BRAVE_MCP_HOST`: HTTP server host (default: "0.0.0.0") - `BRAVE_MCP_LOG_LEVEL`: Desired logging level("debug", "info", "notice", "warning", "error", "critical", "alert", or "emergency", default: "info") +- `BRAVE_MCP_ENABLED_TOOLS`: When used, specifies a whitelist for supported tools +- `BRAVE_MCP_DISABLED_TOOLS`: When used, specifies a blacklist for supported tools ### Command Line Options @@ -114,6 +116,9 @@ Options: --transport Transport type (default: stdio) --port HTTP server port (default: 8080) --host HTTP server host (default: 0.0.0.0) + --logging-level Desired logging level (one of _debug_, _info_, _notice_, _warning_, _error_, _critical_, _alert_, or _emergency_) + --enabled-tools Tools whitelist (only the specified tools will be enabled) + --disabled-tools Tools blacklist (included tools will be disabled) ``` ## Installation diff --git a/docker-compose.yml b/docker-compose.yml index c5259c5..0482b4b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,6 +13,8 @@ services: - BRAVE_MCP_PORT=8080 - BRAVE_MCP_HOST=0.0.0.0 - BRAVE_MCP_LOG_LEVEL=info + - BRAVE_MCP_ENABLED_TOOLS + - BRAVE_MCP_DISABLED_TOOLS init: true ports: - 8080:8080 diff --git a/src/config.ts b/src/config.ts index 5821325..007d8f8 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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 ', '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 ', + 'tools to enable', + process.env.BRAVE_MCP_ENABLED_TOOLS?.split(' ') ?? [] + ) + .option( + '--disabled-tools ', + 'tools to disable', + process.env.BRAVE_MCP_DISABLED_TOOLS?.split(' ') ?? [] + ) .option( '--port ', '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; diff --git a/src/server.ts b/src/server.ts index 28ae8e2..8624532 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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); }