From 52113e324681c8ebbade50826817284c24c66731 Mon Sep 17 00:00:00 2001 From: Sampson Date: Wed, 2 Jul 2025 18:07:24 -0500 Subject: [PATCH] retrieve api key from config alone improves api key management; no longer retrieving it in different ways from different parts of the application --- src/BraveAPI/index.ts | 4 ++-- src/config.ts | 17 ++++++++++++++++- src/constants.ts | 2 -- src/protocols/http.ts | 10 ++++------ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/BraveAPI/index.ts b/src/BraveAPI/index.ts index 4777b5f..49e9c06 100644 --- a/src/BraveAPI/index.ts +++ b/src/BraveAPI/index.ts @@ -1,5 +1,5 @@ import type { Endpoints } from './types.js'; -import { BRAVE_API_KEY } from '../constants.js'; +import config from '../config.js'; import { checkRateLimit, log, stringify } from '../utils.js'; const typeToPathMap: Record = { @@ -14,7 +14,7 @@ const typeToPathMap: Record = { const defaultRequestHeaders: Record = { Accept: 'application/json', 'Accept-Encoding': 'gzip', - 'X-Subscription-Token': BRAVE_API_KEY, + 'X-Subscription-Token': config.braveApiKey, }; async function issueRequest( diff --git a/src/config.ts b/src/config.ts index 6254c00..dce8502 100644 --- a/src/config.ts +++ b/src/config.ts @@ -7,6 +7,14 @@ type Configuration = { braveApiKey: string; }; +const state: Configuration & { ready: boolean } = { + transport: 'http', + port: 8080, + host: '0.0.0.0', + braveApiKey: process.env.BRAVE_API_KEY ?? '', + ready: false, +}; + export function getOptions(): Configuration | false { const program = new Command() .option('--brave-api-key ', 'Brave API key', process.env.BRAVE_API_KEY ?? '') @@ -54,7 +62,14 @@ export function getOptions(): Configuration | false { } } + // Update state + state.braveApiKey = options.braveApiKey; + state.transport = options.transport; + state.port = options.port; + state.host = options.host; + state.ready = true; + return options as Configuration; } -export default { getOptions }; +export default state; diff --git a/src/constants.ts b/src/constants.ts index 84af341..ae86eef 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,3 @@ -export const BRAVE_API_KEY = process.env.BRAVE_API_KEY!; - export const RATE_LIMIT = { perSecond: 1, perMonth: 15000, diff --git a/src/protocols/http.ts b/src/protocols/http.ts index 46ad161..c354f96 100644 --- a/src/protocols/http.ts +++ b/src/protocols/http.ts @@ -1,6 +1,6 @@ import { randomUUID } from 'node:crypto'; import express, { type Request, type Response } from 'express'; -import { getOptions } from '../config.js'; +import config from '../config.js'; import { server } from '../server.js'; import { registerSigIntHandler } from '../helpers.js'; import { isInitializeRequest } from '@modelcontextprotocol/sdk/types.js'; @@ -37,9 +37,7 @@ const getTransport = async ( }; export const start = () => { - const options = getOptions(); - - if (!options) { + if (!config.ready) { console.error('Invalid configuration'); process.exit(1); } @@ -64,8 +62,8 @@ export const start = () => { } }); - app.listen(options.port, options.host, () => { - console.error(`Server is running on http://${options.host}:${options.port}/mcp`); + app.listen(config.port, config.host, () => { + console.error(`Server is running on http://${config.host}:${config.port}/mcp`); }); // Register a SIGINT handler to close all transports when the server is shut down