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:
1 parent
0bca1a42b3
commit
9f87c8bb93
4 files changed
+50
-1
No files matched your search
@@ -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 <stdio|http> Transport type (default: stdio)
|
||||
--port <number> HTTP server port (default: 8080)
|
||||
--host <string> HTTP server host (default: 0.0.0.0)
|
||||
--logging-level <string> 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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user