Refactor configuration and API; added endpoints for image, video, and news.

This commit is contained in:
Sampson committed 2025-07-02 17:04:22 -05:00
1 parent 7789f05087
commit 8f942b9eee
29 files changed
+2867 -490

No files matched your search

+75 -74
View File
@@ -1,74 +1,75 @@
import { randomUUID } from "node:crypto";
import express, { type Request, type Response } from "express";
import { getOptions } from "../config.js";
import { server } from "../server.js";
import { registerSigIntHandler } from "../helpers.js";
import { isInitializeRequest } from "@modelcontextprotocol/sdk/types.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import { InMemoryEventStore } from "@modelcontextprotocol/sdk/examples/shared/inMemoryEventStore.js";
const getTransport = async (
req: Request,
transports: Map<string, StreamableHTTPServerTransport>,
sessionId?: string | string[]
): Promise<StreamableHTTPServerTransport> => {
// Belongs to an existing session; re-use the transport
if (sessionId && transports.has(sessionId as string)) {
return transports.get(sessionId as string) as StreamableHTTPServerTransport;
}
// No session ID and this is an initialization request; create a new transport
if (!sessionId && isInitializeRequest(req.body)) {
const transport = new StreamableHTTPServerTransport({
eventStore: new InMemoryEventStore(),
sessionIdGenerator: () => randomUUID(),
onsessioninitialized: (sessionId) => transports.set(sessionId, transport)
});
transport.onclose = () => transports.delete(transport.sessionId as string);
await server.connect(transport);
return transport;
}
// No session ID and this is not an initialization request; throw an error
throw new Error('Invalid request - no session ID or not initialization request');
}
export const start = () => {
const options = getOptions();
if (!options) {
console.error("Invalid configuration");
process.exit(1);
}
const app = express();
const transports = new Map<string, StreamableHTTPServerTransport>();
app.use(express.json());
app.all('/mcp', async (req: Request, res: Response) => {
try {
const transport = await getTransport(req, transports, req.headers['mcp-session-id']);
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(options.port, options.host, () => {
console.error(`Server is running on http://${options.host}:${options.port}/mcp`);
});
// Register a SIGINT handler to close all transports when the server is shut down
registerSigIntHandler(transports);
}
export default { start };
import { randomUUID } from 'node:crypto';
import express, { type Request, type Response } from 'express';
import { getOptions } from '../config.js';
import { server } from '../server.js';
import { registerSigIntHandler } from '../helpers.js';
import { isInitializeRequest } from '@modelcontextprotocol/sdk/types.js';
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
import { InMemoryEventStore } from '@modelcontextprotocol/sdk/examples/shared/inMemoryEventStore.js';
const getTransport = async (
req: Request,
transports: Map<string, StreamableHTTPServerTransport>,
sessionId?: string | string[]
): Promise<StreamableHTTPServerTransport> => {
// Belongs to an existing session; re-use the transport
if (sessionId && transports.has(sessionId as string)) {
return transports.get(sessionId as string) as StreamableHTTPServerTransport;
}
// No session ID and this is an initialization request; create a new transport
if (!sessionId && isInitializeRequest(req.body)) {
const transport = new StreamableHTTPServerTransport({
eventStore: new InMemoryEventStore(),
sessionIdGenerator: () => randomUUID(),
onsessioninitialized: (sessionId) => transports.set(sessionId, transport),
});
transport.onclose = () => transports.delete(transport.sessionId as string);
await server.connect(transport);
return transport;
}
// No session ID and this is not an initialization request; throw an error
throw new Error('Invalid request - no session ID or not initialization request');
};
export const start = () => {
const options = getOptions();
if (!options) {
console.error('Invalid configuration');
process.exit(1);
}
const app = express();
const transports = new Map<string, StreamableHTTPServerTransport>();
app.use(express.json());
app.all('/mcp', async (req: Request, res: Response) => {
try {
const transport = await getTransport(req, transports, req.headers['mcp-session-id']);
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(options.port, options.host, () => {
console.error(`Server is running on http://${options.host}:${options.port}/mcp`);
});
// Register a SIGINT handler to close all transports when the server is shut down
registerSigIntHandler(transports);
};
export default { start };
+2 -2
View File
@@ -1,2 +1,2 @@
export { default as stdioServer } from "./stdio.js";
export { default as httpServer } from "./http.js";
export { default as stdioServer } from './stdio.js';
export { default as httpServer } from './http.js';
+10 -10
View File
@@ -1,10 +1,10 @@
import { server } from "../server.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
export const start = async (): Promise<void> => {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Stdio server started");
};
export default { start }
import { server } from '../server.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
export const start = async (): Promise<void> => {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error('Stdio server started');
};
export default { start };