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:
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