retrieve api key from config alone

improves api key management; no longer retrieving it in different ways from different parts of the application
This commit is contained in:
Sampson committed 2025-07-02 18:07:24 -05:00
1 parent 8f942b9eee
commit 52113e3246
4 files changed
+22 -11

No files matched your search

+2 -2
View File
@@ -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<keyof Endpoints, string> = {
@@ -14,7 +14,7 @@ const typeToPathMap: Record<keyof Endpoints, string> = {
const defaultRequestHeaders: Record<string, string> = {
Accept: 'application/json',
'Accept-Encoding': 'gzip',
'X-Subscription-Token': BRAVE_API_KEY,
'X-Subscription-Token': config.braveApiKey,
};
async function issueRequest<T extends keyof Endpoints>(
+16 -1
View File
@@ -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 <string>', '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;
-2
View File
@@ -1,5 +1,3 @@
export const BRAVE_API_KEY = process.env.BRAVE_API_KEY!;
export const RATE_LIMIT = {
perSecond: 1,
perMonth: 15000,
+4 -6
View File
@@ -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