diff --git a/README.md b/README.md index 6145e19..5052830 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ Searches for videos with comprehensive metadata and thumbnail information. - `freshness` (string, optional): Time filter ("pd", "pw", "pm", "py", or date range) ### Image Search (`brave_image_search`) -Searches for images with automatic fetching and base64 encoding for direct display. +Searches for images with metadata including URLs, dimensions, and confidence scores. **Parameters:** - `query` (string, required): Search terms (max 400 chars, 50 words) @@ -171,7 +171,7 @@ The server supports the following environment variables: - `BRAVE_API_KEY`: Your Brave Search API key (required) - `BRAVE_MCP_TRANSPORT`: Transport mode ("http" or "stdio", default: "stdio") -- `BRAVE_MCP_PORT`: HTTP server port (default: 8000) +- `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 space-separated whitelist for supported tools diff --git a/package.json b/package.json index d158441..04f0923 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "watch": "tsc --watch", "format": "prettier --write \"src/**/*.ts\"", "format:check": "prettier --check \"src/**/*.ts\"", + "test": "node --import tsx --test \"src/**/*.test.ts\"", "inspector": "npx @modelcontextprotocol/inspector", "inspector:http": "npx @modelcontextprotocol/inspector --transport http" }, diff --git a/src/config.ts b/src/config.ts index 8c9cc70..82c7d1f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2,6 +2,7 @@ import { LoggingLevel, LoggingLevelSchema } from '@modelcontextprotocol/sdk/type import { Command } from 'commander'; import dotenv from 'dotenv'; import tools from './tools/index.js'; +import { parsePort } from './utils.js'; dotenv.config({ debug: false, quiet: true }); @@ -125,12 +126,14 @@ export function getOptions(): Configuration | false { } if (options.transport === 'http') { - if (options.port < 1 || options.port > 65535) { + const port = parsePort(options.port); + if (port === null) { console.error( `Invalid --port value: '${options.port}'. Must be a valid port number between 1 and 65535.` ); return false; } + options.port = port; if (!options.host) { console.error('Error: --host is required'); diff --git a/src/utils.test.ts b/src/utils.test.ts new file mode 100644 index 0000000..1c58b17 --- /dev/null +++ b/src/utils.test.ts @@ -0,0 +1,24 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import { parsePort } from './utils.js'; + +describe('parsePort', () => { + it('accepts valid integer ports', () => { + assert.equal(parsePort(8080), 8080); + assert.equal(parsePort('8080'), 8080); + assert.equal(parsePort(1), 1); + assert.equal(parsePort(65535), 65535); + }); + + it('rejects non-numeric and out-of-range values', () => { + assert.equal(parsePort('abc'), null); + assert.equal(parsePort('8080abc'), null); + assert.equal(parsePort('8080.5'), null); + assert.equal(parsePort(0), null); + assert.equal(parsePort(65536), null); + assert.equal(parsePort(-1), null); + assert.equal(parsePort(''), null); + assert.equal(parsePort(null), null); + assert.equal(parsePort(undefined), null); + }); +}); diff --git a/src/utils.ts b/src/utils.ts index fdda638..352597b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -22,3 +22,28 @@ export function checkRateLimit() { export function stringify(data: any, pretty = false) { return pretty ? JSON.stringify(data, null, 2) : JSON.stringify(data); } + +export function parsePort(value: unknown): number | null { + if (value === undefined || value === null || value === '') { + return null; + } + + if (typeof value === 'number') { + if (!Number.isInteger(value) || value < 1 || value > 65535) { + return null; + } + return value; + } + + const text = String(value).trim(); + if (!/^\d+$/.test(text)) { + return null; + } + + const parsed = Number.parseInt(text, 10); + if (parsed < 1 || parsed > 65535) { + return null; + } + + return parsed; +}