#!/usr/bin/env node
import process from 'node:process';
import { BraveMcpServer } from './server.js';
// Check for API key
const BRAVE_API_KEY = process.env.BRAVE_API_KEY;
if (!BRAVE_API_KEY) {
console.error('Error: BRAVE_API_KEY environment variable is required');
process.exit(1);
}
// Type definitions for Cloudflare Workers (if using Cloudflare)
interface Env {
// Add your environment variables here if needed
}
interface ExecutionContext {
waitUntil(promise: Promise<any>): void;
passThroughOnException(): void;
}
export class MyMCP extends BraveMcpServer {
constructor() {
// Use non-null assertion since we've already checked above
super(BRAVE_API_KEY!);
}
}
export default {
fetch(request: Request, env: Env, ctx: ExecutionContext) {
const url = new URL(request.url);
if (url.pathname === "/sse" || url.pathname === "/sse/message") {
return MyMCP.serveSSE("/sse").fetch(request, env, ctx);
}
if (url.pathname === "/mcp") {
return MyMCP.serve("/mcp").fetch(request, env, ctx);
}
return new Response("Not found", { status: 404 });
},
};
// For Node.js execution (existing functionality)
if (typeof window === 'undefined' && typeof global !== 'undefined') {
const braveMcpServer = new BraveMcpServer(BRAVE_API_KEY!);
braveMcpServer.init().then(() => {
return braveMcpServer.start();
}).catch((error) => {
console.error('Error starting server:', error);
process.exit(1);
});
}