Skip to content

Commit 1d7f200

Browse files
committed
chore: refactors authenticateApiKey into shared util
Signed-off-by: Anthony D. Mays <[email protected]>
1 parent 9032c09 commit 1d7f200

File tree

4 files changed

+27
-38
lines changed

4 files changed

+27
-38
lines changed

lib/javascript/fullstack_demo/mcp-server/src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class TodoMCPServer {
4343

4444
private setupErrorHandling(): void {
4545
this.server.onerror = (error: Error) => {
46-
console.error('[MCP Error]', error);
46+
console.debug('[MCP Error]', error);
4747
};
4848

4949
process.on('SIGINT', async () => {
@@ -208,9 +208,9 @@ class TodoMCPServer {
208208
}
209209

210210
private async getTodos() {
211-
console.error('[MCP] Getting todos...');
211+
console.debug('[MCP] Getting todos...');
212212
const todos = await this.apiClient.getTodos();
213-
console.error(`[MCP] Retrieved ${todos.length} todos`);
213+
console.debug(`[MCP] Retrieved ${todos.length} todos`);
214214

215215
return {
216216
content: [
@@ -223,9 +223,9 @@ class TodoMCPServer {
223223
}
224224

225225
private async createTodo(args: CreateTodoRequest) {
226-
console.error(`[MCP] Creating todo: "${args.text}"`);
226+
console.debug(`[MCP] Creating todo: "${args.text}"`);
227227
const result = await this.apiClient.createTodo(args.text, args.completed);
228-
console.error(`[MCP] Todo created with ID: ${result}`);
228+
console.debug(`[MCP] Todo created with ID: ${result}`);
229229

230230
return {
231231
content: [
@@ -302,11 +302,11 @@ class TodoMCPServer {
302302
async run(): Promise<void> {
303303
const transport = new StdioServerTransport();
304304
await this.server.connect(transport);
305-
console.error('Todo MCP server running on stdio');
306-
console.error(
305+
console.log('Todo MCP server running on stdio');
306+
console.debug(
307307
`API Base URL: ${process.env.TODO_API_BASE_URL || 'http://localhost:3000/api/mcp'}`,
308308
);
309-
console.error(
309+
console.debug(
310310
`API Key configured: ${process.env.MCP_API_KEY ? 'Yes' : 'No'}`,
311311
);
312312
}

lib/javascript/fullstack_demo/src/app/api/mcp/todos/[id]/route.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
import { createTodoRepository } from '@/repositories';
2+
import { authenticateApiKey } from '@/util/mcp-auth';
23
import { NextResponse } from 'next/server';
34

45
const todoRepository = createTodoRepository();
56

6-
// Simple API key authentication for MCP
7-
function authenticateApiKey(request: Request): string | null {
8-
const apiKey =
9-
request.headers.get('X-API-Key') ||
10-
request.headers.get('Authorization')?.replace('Bearer ', '');
11-
const validApiKey = process.env.MCP_API_KEY;
12-
13-
if (!validApiKey || !apiKey || apiKey !== validApiKey) {
14-
return null;
15-
}
16-
17-
return process.env.MCP_DEFAULT_USER_ID || 'mcp-user';
18-
}
19-
207
/**
218
* Delete a todo for MCP client
229
*/

lib/javascript/fullstack_demo/src/app/api/mcp/todos/route.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
import { createTodoRepository } from '@/repositories';
2+
import { authenticateApiKey } from '@/util/mcp-auth';
23
import { NextResponse } from 'next/server';
34

45
const todoRepository = createTodoRepository();
56

6-
// Simple API key authentication for MCP
7-
function authenticateApiKey(request: Request): string | null {
8-
const apiKey =
9-
request.headers.get('X-API-Key') ||
10-
request.headers.get('Authorization')?.replace('Bearer ', '');
11-
const validApiKey = process.env.MCP_API_KEY;
12-
13-
if (!validApiKey || !apiKey || apiKey !== validApiKey) {
14-
return null;
15-
}
16-
17-
// For demo purposes, return a default user ID
18-
// In production, you might map API keys to specific users
19-
return process.env.MCP_DEFAULT_USER_ID || 'mcp-user';
20-
}
21-
227
/**
238
* Retrieve all todos for MCP client
249
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Simple API key authentication for MCP
3+
*/
4+
export function authenticateApiKey(request: Request): string | null {
5+
const apiKey =
6+
request.headers.get('X-API-Key') ||
7+
request.headers.get('Authorization')?.replace('Bearer ', '');
8+
const validApiKey = process.env.MCP_API_KEY;
9+
10+
if (!validApiKey || !apiKey || apiKey !== validApiKey) {
11+
return null;
12+
}
13+
14+
// For demo purposes, return a default user ID
15+
// In production, you might map API keys to specific users
16+
return process.env.MCP_DEFAULT_USER_ID || 'mcp-user';
17+
}

0 commit comments

Comments
 (0)