Skip to content

Commit d231612

Browse files
committed
chore: handle api key internally
1 parent 18a47d3 commit d231612

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/index.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { AVAILABLE_TOOLS } from "./tools/index.js";
1111
export class TokenMetricsMCPServer {
1212
readonly server: Server;
1313

14-
constructor() {
14+
constructor(apiKey?: string) {
1515
this.server = new Server(
1616
{
1717
name: "Token Metrics MCP Server",
@@ -24,10 +24,10 @@ export class TokenMetricsMCPServer {
2424
},
2525
);
2626

27-
this.setupToolHandlers();
27+
this.setupToolHandlers(apiKey);
2828
}
2929

30-
private setupToolHandlers(): void {
30+
private setupToolHandlers(apiKey?: string): void {
3131
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
3232
return {
3333
tools: AVAILABLE_TOOLS.map((tool) => tool.getToolDefinition()),
@@ -47,7 +47,10 @@ export class TokenMetricsMCPServer {
4747
throw new Error(`Unknown tool: ${name}`);
4848
}
4949

50-
return await tool.execute(args);
50+
const toolClass = Object.getPrototypeOf(tool).constructor;
51+
const toolInstance = new toolClass(apiKey);
52+
53+
return await toolInstance.execute(args ?? {});
5154
},
5255
);
5356
}
@@ -65,11 +68,7 @@ export default function createServer({
6568
}: {
6669
config: z.infer<typeof configSchema>;
6770
}) {
68-
if (config.apiKey) {
69-
process.env.TOKEN_METRICS_API_KEY = config.apiKey;
70-
}
71-
72-
const server = new TokenMetricsMCPServer();
71+
const server = new TokenMetricsMCPServer(config.apiKey);
7372
return server.server;
7473
}
7574

0 commit comments

Comments
 (0)