Switch MCP transport from stdio to Streamable HTTP
Mount POST /mcp on the main Express app (stateless Streamable HTTP). Keep optional MCP-only HTTP process via npm run mcp. Update docs.
This commit is contained in:
21
src/index.js
21
src/index.js
@@ -3,11 +3,14 @@ const { WebshareClient } = require('./webshare');
|
||||
const { caps, feed } = require('./torznab');
|
||||
const { makeTorrent } = require('./torrent');
|
||||
const qbt = require('./qbt');
|
||||
const { registerMcpHttp } = require('./mcp-http');
|
||||
|
||||
const PORT = process.env.PORT || 3001;
|
||||
const BASE_URL = process.env.BASE_URL || `http://localhost:${PORT}`;
|
||||
const USERNAME = process.env.WEBSHARE_USERNAME;
|
||||
const PASSWORD = process.env.WEBSHARE_PASSWORD;
|
||||
const MCP_PATH = process.env.MCP_PATH || '/mcp';
|
||||
const MCP_ENABLED = process.env.MCP_ENABLED !== '0' && process.env.MCP_ENABLED !== 'false';
|
||||
|
||||
if (!USERNAME || !PASSWORD) {
|
||||
console.error('WEBSHARE_USERNAME and WEBSHARE_PASSWORD must be set');
|
||||
@@ -18,7 +21,7 @@ const client = new WebshareClient(USERNAME, PASSWORD);
|
||||
const app = express();
|
||||
|
||||
app.use(express.urlencoded({ extended: false }));
|
||||
app.use(express.json());
|
||||
app.use(express.json({ limit: '4mb' }));
|
||||
|
||||
client.ensureAuth().catch(err => console.error('Initial auth failed:', err.message));
|
||||
|
||||
@@ -113,6 +116,15 @@ app.get('/resolve/:ident', async (req, res) => {
|
||||
// ── Fake qBittorrent Web API ──────────────────────────────────────────────
|
||||
qbt.register(app);
|
||||
|
||||
// ── MCP Streamable HTTP (AI agents) ───────────────────────────────────────
|
||||
if (MCP_ENABLED) {
|
||||
registerMcpHttp(app, {
|
||||
username: USERNAME,
|
||||
password: PASSWORD,
|
||||
path: MCP_PATH,
|
||||
});
|
||||
}
|
||||
|
||||
// ── Automatic missing-episode search ─────────────────────────────────────
|
||||
// Sonarr won't find new downloads without being asked (no RSS support).
|
||||
// Every 2 hours, tell Sonarr to search for all missing monitored episodes.
|
||||
@@ -148,4 +160,9 @@ if (SONARR_KEY) {
|
||||
console.log(`[search] auto missing-episode search every ${SEARCH_INTERVAL / 3600000}h`);
|
||||
}
|
||||
|
||||
app.listen(PORT, () => console.log(`webshare-api listening on :${PORT} — base URL: ${BASE_URL}`));
|
||||
app.listen(PORT, () => {
|
||||
console.log(`webshare-api listening on :${PORT} — base URL: ${BASE_URL}`);
|
||||
if (MCP_ENABLED) {
|
||||
console.log(`MCP Streamable HTTP: POST ${BASE_URL.replace(/\/$/, '')}${MCP_PATH}`);
|
||||
}
|
||||
});
|
||||
|
||||
57
src/mcp-http.js
Normal file
57
src/mcp-http.js
Normal file
@@ -0,0 +1,57 @@
|
||||
const { StreamableHTTPServerTransport } = require('@modelcontextprotocol/sdk/server/streamableHttp.js');
|
||||
const { createWebshareMcpServer } = require('./mcp-server');
|
||||
|
||||
/**
|
||||
* Mount a stateless Streamable HTTP MCP endpoint on an Express app.
|
||||
* Default path: POST /mcp (MCP Streamable HTTP transport).
|
||||
*
|
||||
* @param {import('express').Express} app
|
||||
* @param {{ username: string, password: string, path?: string }} options
|
||||
*/
|
||||
function registerMcpHttp(app, { username, password, path: mountPath = '/mcp' }) {
|
||||
const handle = async (req, res) => {
|
||||
// Stateless: one MCP server + transport per request
|
||||
const server = createWebshareMcpServer({ username, password });
|
||||
try {
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: undefined,
|
||||
});
|
||||
await server.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
res.on('close', () => {
|
||||
try {
|
||||
transport.close();
|
||||
} catch { /* ignore */ }
|
||||
try {
|
||||
server.close();
|
||||
} catch { /* ignore */ }
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('MCP HTTP error:', err);
|
||||
if (!res.headersSent) {
|
||||
res.status(500).json({
|
||||
jsonrpc: '2.0',
|
||||
error: { code: -32603, message: 'Internal server error' },
|
||||
id: null,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
app.post(mountPath, handle);
|
||||
|
||||
// Streamable HTTP clients may probe GET/DELETE; this deployment is POST-only (stateless).
|
||||
const methodNotAllowed = (req, res) => {
|
||||
res.status(405).json({
|
||||
jsonrpc: '2.0',
|
||||
error: { code: -32000, message: 'Method not allowed. Use POST for Streamable HTTP (stateless).' },
|
||||
id: null,
|
||||
});
|
||||
};
|
||||
app.get(mountPath, methodNotAllowed);
|
||||
app.delete(mountPath, methodNotAllowed);
|
||||
|
||||
console.log(`MCP Streamable HTTP enabled at POST ${mountPath}`);
|
||||
}
|
||||
|
||||
module.exports = { registerMcpHttp };
|
||||
53
src/mcp.js
53
src/mcp.js
@@ -1,32 +1,39 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* MCP stdio entrypoint for AI agents.
|
||||
* Optional standalone HTTP process that only serves MCP (no Torznab / qBittorrent).
|
||||
* Prefer `npm start` (main app) which mounts MCP at POST /mcp on the same port.
|
||||
*
|
||||
* Configure clients with:
|
||||
* command: node
|
||||
* args: ["…/webshare-api/src/mcp.js"]
|
||||
* env: { WEBSHARE_USERNAME, WEBSHARE_PASSWORD }
|
||||
*
|
||||
* Do not write application logs to stdout — it is reserved for the MCP protocol.
|
||||
* Env:
|
||||
* WEBSHARE_USERNAME, WEBSHARE_PASSWORD (required)
|
||||
* PORT (default 3001)
|
||||
* MCP_PATH (default /mcp)
|
||||
*/
|
||||
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');
|
||||
const { createWebshareMcpServer } = require('./mcp-server');
|
||||
const express = require('express');
|
||||
const { registerMcpHttp } = require('./mcp-http');
|
||||
|
||||
async function main() {
|
||||
const username = process.env.WEBSHARE_USERNAME;
|
||||
const password = process.env.WEBSHARE_PASSWORD;
|
||||
if (!username || !password) {
|
||||
console.error('WEBSHARE_USERNAME and WEBSHARE_PASSWORD must be set');
|
||||
process.exit(1);
|
||||
}
|
||||
const PORT = process.env.PORT || 3001;
|
||||
const MCP_PATH = process.env.MCP_PATH || '/mcp';
|
||||
const USERNAME = process.env.WEBSHARE_USERNAME;
|
||||
const PASSWORD = process.env.WEBSHARE_PASSWORD;
|
||||
|
||||
const server = createWebshareMcpServer({ username, password });
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
console.error('webshare-api MCP server running on stdio');
|
||||
if (!USERNAME || !PASSWORD) {
|
||||
console.error('WEBSHARE_USERNAME and WEBSHARE_PASSWORD must be set');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error('MCP server failed:', err);
|
||||
process.exit(1);
|
||||
const app = express();
|
||||
app.use(express.json({ limit: '4mb' }));
|
||||
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({ ok: true, mcp: MCP_PATH });
|
||||
});
|
||||
|
||||
registerMcpHttp(app, {
|
||||
username: USERNAME,
|
||||
password: PASSWORD,
|
||||
path: MCP_PATH,
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`webshare-api MCP (HTTP) listening on :${PORT}${MCP_PATH}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user