adds http streaming support to server
HTTP streaming is the default. To launch with STDIO the user must pass the `--transport stdio` CLI arguments. This has been documented in the README.md file.
This commit is contained in:
7 files changed
+274
-172
No files matched your search
+122
-80
@@ -1,97 +1,139 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
||||
import { Command } from "commander";
|
||||
import express, { type Request, type Response } from "express";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
||||
import { BRAVE_API_KEY } from "./constants.js";
|
||||
|
||||
import WEB_SEARCH_TOOL, { validateArgs as isBraveWebSearchArgs, performSearch as performWebSearch } from "./tools/web.js";
|
||||
import LOCAL_SEARCH_TOOL, { validateArgs as isBraveLocalSearchArgs, performSearch as performLocalSearch } from "./tools/local.js";
|
||||
import { name as braveWebSearchName, paramsSchema as braveWebSearchParamsSchema, performSearch as performWebSearch } from "./tools/web.js";
|
||||
import { name as braveLocalSearchName, paramsSchema as braveLocalSearchParamsSchema, performSearch as performLocalSearch } from "./tools/local.js";
|
||||
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
|
||||
import { InMemoryEventStore } from "@modelcontextprotocol/sdk/examples/shared/inMemoryEventStore.js";
|
||||
|
||||
// Server implementation
|
||||
const server = new Server(
|
||||
{
|
||||
name: "brave-search-mcp-server",
|
||||
version: "0.1.0",
|
||||
},
|
||||
{
|
||||
capabilities: {
|
||||
tools: {},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
// Check for API key
|
||||
if (!BRAVE_API_KEY) {
|
||||
console.error("Error: BRAVE_API_KEY environment variable is required");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Tool handlers
|
||||
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
||||
tools: [WEB_SEARCH_TOOL, LOCAL_SEARCH_TOOL],
|
||||
}));
|
||||
const program = new Command()
|
||||
.option("--transport <stdio|http>", "transport type", "http")
|
||||
.option("--port <number>", "desired port for HTTP transport", "3000")
|
||||
.allowUnknownOption()
|
||||
.parse(process.argv);
|
||||
|
||||
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
||||
try {
|
||||
const { name, arguments: args } = request.params;
|
||||
const TRANSPORT_TYPES = ["stdio", "http"] as const;
|
||||
|
||||
if (!args) {
|
||||
throw new Error("No arguments provided");
|
||||
}
|
||||
const CLI_OPTIONS = program.opts<{ transport: "stdio" | "http"; port: string }>();
|
||||
|
||||
switch (name) {
|
||||
case "brave_web_search": {
|
||||
if (!isBraveWebSearchArgs(args)) {
|
||||
throw new Error("Invalid arguments for brave_web_search");
|
||||
}
|
||||
const { query, count = 10 } = args;
|
||||
const results = await performWebSearch(query, count);
|
||||
return {
|
||||
content: [{ type: "text", text: results }],
|
||||
isError: false,
|
||||
};
|
||||
}
|
||||
|
||||
case "brave_local_search": {
|
||||
if (!isBraveLocalSearchArgs(args)) {
|
||||
throw new Error("Invalid arguments for brave_local_search");
|
||||
}
|
||||
const { query, count = 5 } = args;
|
||||
const results = await performLocalSearch(query, count);
|
||||
return {
|
||||
content: [{ type: "text", text: results }],
|
||||
isError: false,
|
||||
};
|
||||
}
|
||||
|
||||
default:
|
||||
return {
|
||||
content: [{ type: "text", text: `Unknown tool: ${name}` }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Error: ${error instanceof Error ? error.message : String(error)}`,
|
||||
},
|
||||
],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
async function runServer() {
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
console.error("Brave Search MCP Server running on stdio");
|
||||
if (!TRANSPORT_TYPES.includes(CLI_OPTIONS.transport)) {
|
||||
console.error(`Invalid --transport value: '${CLI_OPTIONS.transport}'. Must be one of: ${TRANSPORT_TYPES.join(", ")}.`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
runServer().catch((error) => {
|
||||
console.error("Fatal error running server:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
function createServer(): McpServer {
|
||||
const server = new McpServer({
|
||||
version: "0.1.0",
|
||||
name: "brave-search-mcp-server",
|
||||
instructions: "Use this server to search the Web for various types of data via the Brave Search API.",
|
||||
}, { capabilities: { logging: {} } });
|
||||
|
||||
server.tool(braveWebSearchName, braveWebSearchParamsSchema.shape, performWebSearch);
|
||||
server.tool(braveLocalSearchName, braveLocalSearchParamsSchema.shape, performLocalSearch);
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
async function createNewHttpTransport(transports: Map<string, StreamableHTTPServerTransport>): Promise<StreamableHTTPServerTransport> {
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
eventStore: new InMemoryEventStore(),
|
||||
sessionIdGenerator: () => randomUUID(),
|
||||
onsessioninitialized: (sessionId) => {
|
||||
transports.set(sessionId, transport);
|
||||
}
|
||||
});
|
||||
|
||||
transport.onclose = () => {
|
||||
const sessionId = transport.sessionId;
|
||||
if (sessionId && transports.has(sessionId)) {
|
||||
transports.delete(sessionId);
|
||||
}
|
||||
};
|
||||
|
||||
return transport;
|
||||
}
|
||||
|
||||
async function getHttpTransportForRequest(req: Request, transports: Map<string, StreamableHTTPServerTransport>): Promise<StreamableHTTPServerTransport> {
|
||||
const sessionId = req.headers['mcp-session-id'] as string | undefined;
|
||||
|
||||
if (sessionId && transports.has(sessionId)) {
|
||||
return transports.get(sessionId) as StreamableHTTPServerTransport;
|
||||
}
|
||||
|
||||
if (!sessionId && isInitializeRequest(req.body)) {
|
||||
const server = createServer();
|
||||
const transport = await createNewHttpTransport(transports);
|
||||
await server.connect(transport);
|
||||
|
||||
return transport;
|
||||
}
|
||||
|
||||
throw new Error('Invalid request - no session ID or not initialization request');
|
||||
}
|
||||
|
||||
function startHttpServer(port: string, transports: Map<string, StreamableHTTPServerTransport>) {
|
||||
const app = express();
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
app.all('/mcp', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const transport = await getHttpTransportForRequest(req, transports);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
} catch (error) {
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({
|
||||
id: null, jsonrpc: '2.0',
|
||||
error: { code: -32603, message: 'Internal server error' },
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => console.error(`Server is running on http://localhost:${port}/mcp`));
|
||||
}
|
||||
|
||||
async function startStdioServer() {
|
||||
const server = createServer();
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
}
|
||||
|
||||
function registerSigIntHandler(transports: Map<string, StreamableHTTPServerTransport>) {
|
||||
process.on('SIGINT', async () => {
|
||||
for (const sessionID of transports.keys()) {
|
||||
await transports.get(sessionID)?.close();
|
||||
transports.delete(sessionID);
|
||||
}
|
||||
|
||||
console.error('Server shut down.');
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
function main() {
|
||||
const transports = new Map<string, StreamableHTTPServerTransport>();
|
||||
|
||||
if (CLI_OPTIONS.transport === "http") {
|
||||
startHttpServer(CLI_OPTIONS.port, transports);
|
||||
} else {
|
||||
startStdioServer();
|
||||
}
|
||||
|
||||
registerSigIntHandler(transports);
|
||||
console.error(`Server is running on ${CLI_OPTIONS.transport} mode`);
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in new issue
Block a user