feat: adds proper logging support
This commit is contained in:
13 files changed
+187
-38
No files matched your search
@@ -102,6 +102,7 @@ The server supports the following environment variables:
|
|||||||
- `BRAVE_MCP_TRANSPORT`: Transport mode ("http" or "stdio", default: "http")
|
- `BRAVE_MCP_TRANSPORT`: Transport mode ("http" or "stdio", default: "http")
|
||||||
- `BRAVE_MCP_PORT`: HTTP server port (default: 8080)
|
- `BRAVE_MCP_PORT`: HTTP server port (default: 8080)
|
||||||
- `BRAVE_MCP_HOST`: HTTP server host (default: "0.0.0.0")
|
- `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")
|
||||||
|
|
||||||
### Command Line Options
|
### Command Line Options
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ services:
|
|||||||
- BRAVE_MCP_TRANSPORT=http
|
- BRAVE_MCP_TRANSPORT=http
|
||||||
- BRAVE_MCP_PORT=8080
|
- BRAVE_MCP_PORT=8080
|
||||||
- BRAVE_MCP_HOST=0.0.0.0
|
- BRAVE_MCP_HOST=0.0.0.0
|
||||||
|
- BRAVE_MCP_LOG_LEVEL=info
|
||||||
init: true
|
init: true
|
||||||
ports:
|
ports:
|
||||||
- 8080:8080
|
- 8080:8080
|
||||||
|
|||||||
+2
-1
@@ -29,7 +29,8 @@
|
|||||||
"watch": "tsc --watch",
|
"watch": "tsc --watch",
|
||||||
"format": "prettier --write \"src/**/*.ts\"",
|
"format": "prettier --write \"src/**/*.ts\"",
|
||||||
"format:check": "prettier --check \"src/**/*.ts\"",
|
"format:check": "prettier --check \"src/**/*.ts\"",
|
||||||
"inspector": "npx @modelcontextprotocol/inspector"
|
"inspector": "npx @modelcontextprotocol/inspector",
|
||||||
|
"inspector:stdio": "npx @modelcontextprotocol/inspector --transport stdio"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@modelcontextprotocol/sdk": "1.17.2",
|
"@modelcontextprotocol/sdk": "1.17.2",
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import type { Endpoints } from './types.js';
|
import type { Endpoints } from './types.js';
|
||||||
import config from '../config.js';
|
import config from '../config.js';
|
||||||
import { checkRateLimit, log, stringify } from '../utils.js';
|
import { stringify } from '../utils.js';
|
||||||
|
import ClientLogger from '../ClientLogger.js';
|
||||||
|
import { mcpServer } from '../server.js';
|
||||||
|
|
||||||
const typeToPathMap: Record<keyof Endpoints, string> = {
|
const typeToPathMap: Record<keyof Endpoints, string> = {
|
||||||
images: '/res/v1/images/search',
|
images: '/res/v1/images/search',
|
||||||
@@ -31,7 +33,7 @@ async function issueRequest<T extends keyof Endpoints>(
|
|||||||
const url = new URL(`https://api.search.brave.com${typeToPathMap[endpoint]}`);
|
const url = new URL(`https://api.search.brave.com${typeToPathMap[endpoint]}`);
|
||||||
const queryParams = new URLSearchParams();
|
const queryParams = new URLSearchParams();
|
||||||
|
|
||||||
await log('info', `Preparing to issue request to ${url.toString()}`);
|
await ClientLogger.log('info', `Preparing to issue request to ${url.toString()}`);
|
||||||
|
|
||||||
// TODO (Sampson): Move param-construction/validation to modules
|
// TODO (Sampson): Move param-construction/validation to modules
|
||||||
for (const [key, value] of Object.entries(parameters)) {
|
for (const [key, value] of Object.entries(parameters)) {
|
||||||
@@ -78,14 +80,14 @@ async function issueRequest<T extends keyof Endpoints>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await log('debug', `Using parameters: ${queryParams.toString()}`);
|
await ClientLogger.log('debug', `Using parameters: ${queryParams.toString()}`);
|
||||||
|
|
||||||
// Issue Request
|
// Issue Request
|
||||||
const urlWithParams = url.toString() + '?' + queryParams.toString();
|
const urlWithParams = url.toString() + '?' + queryParams.toString();
|
||||||
const headers = { ...defaultRequestHeaders, ...requestHeaders } as Headers;
|
const headers = { ...defaultRequestHeaders, ...requestHeaders } as Headers;
|
||||||
const response = await fetch(urlWithParams, { headers });
|
const response = await fetch(urlWithParams, { headers });
|
||||||
|
|
||||||
await log('debug', `Received response from ${urlWithParams}`);
|
await ClientLogger.log('debug', `Received response from ${urlWithParams}`);
|
||||||
|
|
||||||
// Handle Error
|
// Handle Error
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -98,7 +100,7 @@ async function issueRequest<T extends keyof Endpoints>(
|
|||||||
errorMessage += `\n${await response.text()}`;
|
errorMessage += `\n${await response.text()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
await log('error', errorMessage);
|
await ClientLogger.log('error', errorMessage);
|
||||||
|
|
||||||
// TODO (Sampson): Setup proper error handling, updating state, etc.
|
// TODO (Sampson): Setup proper error handling, updating state, etc.
|
||||||
throw new Error(errorMessage);
|
throw new Error(errorMessage);
|
||||||
@@ -106,7 +108,7 @@ async function issueRequest<T extends keyof Endpoints>(
|
|||||||
|
|
||||||
// Return Response
|
// Return Response
|
||||||
const responseBody = await response.json();
|
const responseBody = await response.json();
|
||||||
await log('debug', `Returning response: ${stringify(responseBody, true)}`);
|
await ClientLogger.log('debug', `Returning response: ${stringify(responseBody, true)}`);
|
||||||
|
|
||||||
return responseBody as Endpoints[T]['response'];
|
return responseBody as Endpoints[T]['response'];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,89 @@
|
|||||||
|
// ClientLogger.ts
|
||||||
|
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
||||||
|
import type { LoggingLevel, SetLevelRequest } from '@modelcontextprotocol/sdk/types.js';
|
||||||
|
import { LoggingLevelSchema, SetLevelRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
||||||
|
import config from './config.js';
|
||||||
|
|
||||||
|
type LoggerFn = (level: LoggingLevel, message: string) => Promise<void>;
|
||||||
|
|
||||||
|
let mcpServer: McpServer | undefined;
|
||||||
|
|
||||||
|
const setServer = (server: McpServer) => {
|
||||||
|
mcpServer = server;
|
||||||
|
};
|
||||||
|
|
||||||
|
let customLogger: LoggerFn | undefined;
|
||||||
|
let currentLevel: number = LoggingLevelSchema.options.indexOf(config.loggingLevel);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to register a custom set level request handler.
|
||||||
|
* If the method is already handled by the SDK/Server, we'll ignore the error.
|
||||||
|
* @see https://github.com/modelcontextprotocol/typescript-sdk/issues/871
|
||||||
|
* @param server {Server} The server to register the handler on.
|
||||||
|
*/
|
||||||
|
export const maybeRegisterCustomSetLevelRequestHandler = (server: McpServer['server']) => {
|
||||||
|
try {
|
||||||
|
server.assertCanSetRequestHandler(SetLevelRequestSchema.shape.method.value);
|
||||||
|
server.setRequestHandler(
|
||||||
|
SetLevelRequestSchema,
|
||||||
|
async (request: SetLevelRequest): Promise<any> => {
|
||||||
|
await log('info', `Setting logging level to ${request.params.level}`);
|
||||||
|
currentLevel = LoggingLevelSchema.options.indexOf(request.params.level);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(
|
||||||
|
`Failed to register custom SetLevelRequest handler. The SDK may now provide it's own handler. See https://github.com/modelcontextprotocol/typescript-sdk/issues/871 for more information.`,
|
||||||
|
error
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const log = async (level: LoggingLevel, message: string) => {
|
||||||
|
// If a custom logger exists, call it, and let it handle it's own log-level filtering
|
||||||
|
if (customLogger) {
|
||||||
|
await customLogger(level, message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the log-level is less than the current log-level, skip it
|
||||||
|
if (LoggingLevelSchema.options.indexOf(level) < currentLevel) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall back to default logger if no custom logger is set
|
||||||
|
const time = new Date().toISOString();
|
||||||
|
|
||||||
|
if (!mcpServer?.isConnected()) {
|
||||||
|
console.error(`${time} [${level}] ${message}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await mcpServer?.server.sendLoggingMessage({ level, data: { message, time } });
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error sending logging message: ${error}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const setLogger = (logger: LoggerFn): void => {
|
||||||
|
customLogger = logger;
|
||||||
|
};
|
||||||
|
|
||||||
|
const setLoggingLevel = (desiredLevel: LoggingLevel): void => {
|
||||||
|
const desiredLevelIndex = LoggingLevelSchema.options.indexOf(desiredLevel);
|
||||||
|
|
||||||
|
if (desiredLevelIndex === -1) {
|
||||||
|
console.error(
|
||||||
|
`Invalid logging level: ${desiredLevel}. Must be one of: ${LoggingLevelSchema.options.join(', ')}`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentLevel = desiredLevelIndex;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getLoggingLevel = (): LoggingLevel => LoggingLevelSchema.options[currentLevel];
|
||||||
|
|
||||||
|
export default { log, setServer, setLogger, setLoggingLevel, getLoggingLevel };
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { LoggingLevel, LoggingLevelSchema } from '@modelcontextprotocol/sdk/types.js';
|
||||||
import { Command } from 'commander';
|
import { Command } from 'commander';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
dotenv.config({ debug: false, quiet: true });
|
dotenv.config({ debug: false, quiet: true });
|
||||||
@@ -7,6 +8,7 @@ type Configuration = {
|
|||||||
port: number;
|
port: number;
|
||||||
host: string;
|
host: string;
|
||||||
braveApiKey: string;
|
braveApiKey: string;
|
||||||
|
loggingLevel: LoggingLevel;
|
||||||
};
|
};
|
||||||
|
|
||||||
const state: Configuration & { ready: boolean } = {
|
const state: Configuration & { ready: boolean } = {
|
||||||
@@ -14,12 +16,14 @@ const state: Configuration & { ready: boolean } = {
|
|||||||
port: 8080,
|
port: 8080,
|
||||||
host: '0.0.0.0',
|
host: '0.0.0.0',
|
||||||
braveApiKey: process.env.BRAVE_API_KEY ?? '',
|
braveApiKey: process.env.BRAVE_API_KEY ?? '',
|
||||||
|
loggingLevel: 'info',
|
||||||
ready: false,
|
ready: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getOptions(): Configuration | false {
|
export function getOptions(): Configuration | false {
|
||||||
const program = new Command()
|
const program = new Command()
|
||||||
.option('--brave-api-key <string>', 'Brave API key', process.env.BRAVE_API_KEY ?? '')
|
.option('--brave-api-key <string>', 'Brave API key', process.env.BRAVE_API_KEY ?? '')
|
||||||
|
.option('--logging-level <string>', 'Logging level', process.env.BRAVE_MCP_LOG_LEVEL ?? 'info')
|
||||||
.option('--transport <stdio|http>', 'transport type', process.env.BRAVE_MCP_TRANSPORT ?? 'http')
|
.option('--transport <stdio|http>', 'transport type', process.env.BRAVE_MCP_TRANSPORT ?? 'http')
|
||||||
.option(
|
.option(
|
||||||
'--port <number>',
|
'--port <number>',
|
||||||
@@ -43,6 +47,13 @@ export function getOptions(): Configuration | false {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!LoggingLevelSchema.options.includes(options.loggingLevel)) {
|
||||||
|
console.error(
|
||||||
|
`Invalid --logging-level value: '${options.loggingLevel}'. Must be one of: ${LoggingLevelSchema.options.join(', ')}`
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!options.braveApiKey) {
|
if (!options.braveApiKey) {
|
||||||
console.error(
|
console.error(
|
||||||
'Error: --brave-api-key is required. You can get one at https://brave.com/search/api/.'
|
'Error: --brave-api-key is required. You can get one at https://brave.com/search/api/.'
|
||||||
@@ -69,6 +80,7 @@ export function getOptions(): Configuration | false {
|
|||||||
state.transport = options.transport;
|
state.transport = options.transport;
|
||||||
state.port = options.port;
|
state.port = options.port;
|
||||||
state.host = options.host;
|
state.host = options.host;
|
||||||
|
state.loggingLevel = options.loggingLevel;
|
||||||
state.ready = true;
|
state.ready = true;
|
||||||
|
|
||||||
return options as Configuration;
|
return options as Configuration;
|
||||||
|
|||||||
+52
-12
@@ -1,7 +1,9 @@
|
|||||||
|
import { randomUUID } from 'node:crypto';
|
||||||
import express, { type Request, type Response } from 'express';
|
import express, { type Request, type Response } from 'express';
|
||||||
import config from '../config.js';
|
import config from '../config.js';
|
||||||
import { server } from '../server.js';
|
import { mcpServer } from '../server.js';
|
||||||
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
||||||
|
import { isInitializeRequest, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
||||||
|
|
||||||
const yieldGenericServerError = (res: Response) => {
|
const yieldGenericServerError = (res: Response) => {
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
@@ -11,26 +13,53 @@ const yieldGenericServerError = (res: Response) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const start = () => {
|
const transports = new Map<string, StreamableHTTPServerTransport>();
|
||||||
if (!config.ready) {
|
|
||||||
console.error('Invalid configuration');
|
export const getTransport = async (request: Request): Promise<StreamableHTTPServerTransport> => {
|
||||||
process.exit(1);
|
// Check for an existing session
|
||||||
|
const sessionId = request.headers['mcp-session-id'] as string;
|
||||||
|
|
||||||
|
if (sessionId && transports.has(sessionId)) {
|
||||||
|
return transports.get(sessionId)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Is the client attempting to initialize a new session?
|
||||||
|
if (isInitializeRequest(request.body)) {
|
||||||
|
const transport = new StreamableHTTPServerTransport({
|
||||||
|
sessionIdGenerator: () => randomUUID(),
|
||||||
|
onsessioninitialized: (sessionId) => {
|
||||||
|
transports.set(sessionId, transport);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await mcpServer.connect(transport);
|
||||||
|
return transport;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We have a special case where we'll permit ListToolsRequest w/o a session ID
|
||||||
|
if (request.body.method === ListToolsRequestSchema.shape.method.value) {
|
||||||
|
const transport = new StreamableHTTPServerTransport({
|
||||||
|
sessionIdGenerator: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
await mcpServer.connect(transport);
|
||||||
|
return transport;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('Invalid request');
|
||||||
|
};
|
||||||
|
|
||||||
|
export const createApp = () => {
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
app.all('/mcp', async (req: Request, res: Response) => {
|
app.all('/mcp', async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const transport = new StreamableHTTPServerTransport({
|
const transport = await getTransport(req);
|
||||||
// Setting to undefined will opt-out of session-id generation
|
|
||||||
sessionIdGenerator: undefined,
|
|
||||||
});
|
|
||||||
|
|
||||||
await server.connect(transport);
|
|
||||||
await transport.handleRequest(req, res, req.body);
|
await transport.handleRequest(req, res, req.body);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
if (!res.headersSent) {
|
if (!res.headersSent) {
|
||||||
yieldGenericServerError(res);
|
yieldGenericServerError(res);
|
||||||
}
|
}
|
||||||
@@ -41,9 +70,20 @@ export const start = () => {
|
|||||||
res.status(200).json({ message: 'pong' });
|
res.status(200).json({ message: 'pong' });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return app;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const start = () => {
|
||||||
|
if (!config.ready) {
|
||||||
|
console.error('Invalid configuration');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const app = createApp();
|
||||||
|
|
||||||
app.listen(config.port, config.host, () => {
|
app.listen(config.port, config.host, () => {
|
||||||
console.error(`Server is running on http://${config.host}:${config.port}/mcp`);
|
console.error(`Server is running on http://${config.host}:${config.port}/mcp`);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export default { start };
|
export default { start, createApp };
|
||||||
@@ -1,10 +1,14 @@
|
|||||||
import { server } from '../server.js';
|
import { mcpServer } from '../server.js';
|
||||||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
||||||
|
|
||||||
|
export const createTransport = (): StdioServerTransport => {
|
||||||
|
return new StdioServerTransport();
|
||||||
|
};
|
||||||
|
|
||||||
export const start = async (): Promise<void> => {
|
export const start = async (): Promise<void> => {
|
||||||
const transport = new StdioServerTransport();
|
const transport = createTransport();
|
||||||
await server.connect(transport);
|
await mcpServer.connect(transport);
|
||||||
console.error('Stdio server started');
|
console.error('Stdio server started');
|
||||||
};
|
};
|
||||||
|
|
||||||
export default { start };
|
export default { start, createTransport };
|
||||||
+7
-2
@@ -1,7 +1,8 @@
|
|||||||
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
||||||
import tools from './tools/index.js';
|
import tools from './tools/index.js';
|
||||||
|
import ClientLogger, { maybeRegisterCustomSetLevelRequestHandler } from './ClientLogger.js';
|
||||||
|
|
||||||
export const server = new McpServer(
|
export const mcpServer = new McpServer(
|
||||||
{
|
{
|
||||||
version: '0.1.0',
|
version: '0.1.0',
|
||||||
name: 'brave-search-mcp-server',
|
name: 'brave-search-mcp-server',
|
||||||
@@ -16,6 +17,10 @@ export const server = new McpServer(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
ClientLogger.setServer(mcpServer);
|
||||||
|
// https://github.com/modelcontextprotocol/typescript-sdk/issues/871
|
||||||
|
maybeRegisterCustomSetLevelRequestHandler(mcpServer.server);
|
||||||
|
|
||||||
for (const tool of Object.values(tools)) {
|
for (const tool of Object.values(tools)) {
|
||||||
server.tool(tool.name, tool.description, tool.inputSchema, tool.annotations, tool.execute);
|
mcpServer.tool(tool.name, tool.description, tool.inputSchema, tool.annotations, tool.execute);
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,8 @@ import type {
|
|||||||
} from '@modelcontextprotocol/sdk/types.js';
|
} from '@modelcontextprotocol/sdk/types.js';
|
||||||
import params, { type QueryParams } from './params.js';
|
import params, { type QueryParams } from './params.js';
|
||||||
import API from '../../BraveAPI/index.js';
|
import API from '../../BraveAPI/index.js';
|
||||||
import { log, stringify } from '../../utils.js';
|
import { stringify } from '../../utils.js';
|
||||||
|
import ClientLogger from '../../ClientLogger.js';
|
||||||
|
|
||||||
export const name = 'brave_image_search';
|
export const name = 'brave_image_search';
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ export const execute = async (params: QueryParams) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function fetchImage(url: string): Promise<{ mimeType: string; data: string } | null> {
|
async function fetchImage(url: string): Promise<{ mimeType: string; data: string } | null> {
|
||||||
await log('info', `Fetching image data from ${url}`);
|
await ClientLogger.log('info', `Fetching image data from ${url}`);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url);
|
const response = await fetch(url);
|
||||||
const buffer = await response.arrayBuffer();
|
const buffer = await response.arrayBuffer();
|
||||||
@@ -53,7 +54,7 @@ async function fetchImage(url: string): Promise<{ mimeType: string; data: string
|
|||||||
mimeType: response.headers.get('content-type') ?? 'image/jpeg',
|
mimeType: response.headers.get('content-type') ?? 'image/jpeg',
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await log('error', `Error fetching image data from ${url}: ${error}`);
|
await ClientLogger.log('error', `Error fetching image data from ${url}: ${error}`);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import webParams, { type QueryParams as WebQueryParams } from '../web/params.js'
|
|||||||
import type { CallToolResult, ToolAnnotations } from '@modelcontextprotocol/sdk/types.js';
|
import type { CallToolResult, ToolAnnotations } from '@modelcontextprotocol/sdk/types.js';
|
||||||
import API from '../../BraveAPI/index.js';
|
import API from '../../BraveAPI/index.js';
|
||||||
import { formatWebResults } from '../web/index.js';
|
import { formatWebResults } from '../web/index.js';
|
||||||
import { log, stringify } from '../../utils.js';
|
import { stringify } from '../../utils.js';
|
||||||
import { WebSearchApiResponse } from '../web/types.js';
|
import { WebSearchApiResponse } from '../web/types.js';
|
||||||
|
|
||||||
export const name = 'brave_local_search';
|
export const name = 'brave_local_search';
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { summarizerQueryParams, type SummarizerQueryParams } from './params.js';
|
|||||||
import API from '../../BraveAPI/index.js';
|
import API from '../../BraveAPI/index.js';
|
||||||
import { type QueryParams as WebQueryParams } from '../web/params.js';
|
import { type QueryParams as WebQueryParams } from '../web/params.js';
|
||||||
import { SummarizerSearchApiResponse } from './types.js';
|
import { SummarizerSearchApiResponse } from './types.js';
|
||||||
import { log } from '../../utils.js';
|
import ClientLogger from '../../ClientLogger.js';
|
||||||
|
|
||||||
export const name = 'brave_summarizer';
|
export const name = 'brave_summarizer';
|
||||||
|
|
||||||
@@ -82,7 +82,7 @@ const pollForSummary = async (
|
|||||||
result = response;
|
result = response;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
await log('error', `Error polling for summarizer results: ${error}`);
|
await ClientLogger.log('error', `Error polling for summarizer results: ${error}`);
|
||||||
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
import type { LoggingLevel } from '@modelcontextprotocol/sdk/types.js';
|
|
||||||
import { RATE_LIMIT } from './constants.js';
|
import { RATE_LIMIT } from './constants.js';
|
||||||
import { server } from './server.js';
|
|
||||||
|
|
||||||
let requestCount = {
|
let requestCount = {
|
||||||
second: 0,
|
second: 0,
|
||||||
@@ -8,11 +6,6 @@ let requestCount = {
|
|||||||
lastReset: Date.now(),
|
lastReset: Date.now(),
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function log(level: LoggingLevel, message: string) {
|
|
||||||
const time = new Date().toISOString();
|
|
||||||
await server.server.sendLoggingMessage({ level, data: { message, time } });
|
|
||||||
}
|
|
||||||
|
|
||||||
export function checkRateLimit() {
|
export function checkRateLimit() {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (now - requestCount.lastReset > 1000) {
|
if (now - requestCount.lastReset > 1000) {
|
||||||
|
|||||||
Reference in new issue
Block a user