mcp-optimizer 0.0.3-alpha.1 → 0.0.5-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mcpServer.js +32 -0
- package/package.json +1 -1
- package/src/mcpServer.ts +31 -0
package/dist/mcpServer.js
CHANGED
|
@@ -39,6 +39,7 @@ const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
|
39
39
|
const zod_1 = require("zod");
|
|
40
40
|
const lighthouseRunner_1 = require("./runner/lighthouseRunner");
|
|
41
41
|
const sse_js_1 = require("@modelcontextprotocol/sdk/server/sse.js");
|
|
42
|
+
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
42
43
|
const http = __importStar(require("http"));
|
|
43
44
|
const fixer_1 = require("./fix/fixer");
|
|
44
45
|
class LighthouseMcpServer {
|
|
@@ -240,6 +241,37 @@ async function startMcpServer() {
|
|
|
240
241
|
const port = Number(portEnv || 5000);
|
|
241
242
|
const mcp = new LighthouseMcpServer();
|
|
242
243
|
let sseTransport = null;
|
|
244
|
+
let stdioTransport = null;
|
|
245
|
+
// If process stdin/stdout appear to be non-TTY pipes, assume we're being
|
|
246
|
+
// launched as a stdio child by an MCP host and use the stdio transport.
|
|
247
|
+
const shouldUseStdio = (process.stdin && !process.stdin.isTTY) && (process.stdout && !process.stdout.isTTY);
|
|
248
|
+
if (shouldUseStdio) {
|
|
249
|
+
// Redirect console output to stderr to avoid corrupting the JSON stdio protocol
|
|
250
|
+
const _orig = { log: console.log, info: console.info, warn: console.warn };
|
|
251
|
+
console.log = (...args) => { process.stderr.write(args.map(String).join(' ') + '\n'); };
|
|
252
|
+
console.info = console.log;
|
|
253
|
+
console.warn = console.log;
|
|
254
|
+
try {
|
|
255
|
+
stdioTransport = new stdio_js_1.StdioServerTransport(process.stdin, process.stdout);
|
|
256
|
+
await mcp.connect(stdioTransport);
|
|
257
|
+
console.error('Stdio: connected to parent process over stdio');
|
|
258
|
+
// When running over stdio we do not start the HTTP server; stay alive
|
|
259
|
+
// and let the parent coordinate messages. Return a promise that
|
|
260
|
+
// resolves when the transport closes.
|
|
261
|
+
return new Promise((resolve, reject) => {
|
|
262
|
+
stdioTransport.onclose = () => resolve();
|
|
263
|
+
stdioTransport.onerror = (err) => reject(err);
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
catch (err) {
|
|
267
|
+
console.error('Stdio: failed to start transport, falling back to HTTP:', err);
|
|
268
|
+
stdioTransport = null;
|
|
269
|
+
// restore console in case we fall back to HTTP server mode
|
|
270
|
+
console.log = _orig.log;
|
|
271
|
+
console.info = _orig.info;
|
|
272
|
+
console.warn = _orig.warn;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
243
275
|
const pendingPosts = [];
|
|
244
276
|
const { Readable, Writable } = await (async () => {
|
|
245
277
|
const mod = await Promise.resolve().then(() => __importStar(require('stream')));
|
package/package.json
CHANGED
package/src/mcpServer.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { z } from "zod";
|
|
|
3
3
|
import { runLighthouseAudit } from "./runner/lighthouseRunner";
|
|
4
4
|
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
5
5
|
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
|
6
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
6
7
|
import * as http from 'http';
|
|
7
8
|
import { IncomingMessage, ServerResponse } from 'http';
|
|
8
9
|
import { autoFixFromReport } from './fix/fixer';
|
|
@@ -225,6 +226,36 @@ export async function startMcpServer(): Promise<void> {
|
|
|
225
226
|
const port = Number(portEnv || 5000);
|
|
226
227
|
const mcp = new LighthouseMcpServer();
|
|
227
228
|
let sseTransport: SSEServerTransport | null = null;
|
|
229
|
+
let stdioTransport: StdioServerTransport | null = null;
|
|
230
|
+
// If process stdin/stdout appear to be non-TTY pipes, assume we're being
|
|
231
|
+
// launched as a stdio child by an MCP host and use the stdio transport.
|
|
232
|
+
const shouldUseStdio = (process.stdin && !process.stdin.isTTY) && (process.stdout && !process.stdout.isTTY);
|
|
233
|
+
if (shouldUseStdio) {
|
|
234
|
+
// Redirect console output to stderr to avoid corrupting the JSON stdio protocol
|
|
235
|
+
const _orig = { log: console.log, info: console.info, warn: console.warn };
|
|
236
|
+
console.log = (...args: any[]) => { process.stderr.write(args.map(String).join(' ') + '\n'); };
|
|
237
|
+
console.info = console.log;
|
|
238
|
+
console.warn = console.log;
|
|
239
|
+
try {
|
|
240
|
+
stdioTransport = new StdioServerTransport(process.stdin, process.stdout);
|
|
241
|
+
await mcp.connect(stdioTransport as unknown as Transport);
|
|
242
|
+
console.error('Stdio: connected to parent process over stdio');
|
|
243
|
+
// When running over stdio we do not start the HTTP server; stay alive
|
|
244
|
+
// and let the parent coordinate messages. Return a promise that
|
|
245
|
+
// resolves when the transport closes.
|
|
246
|
+
return new Promise((resolve, reject) => {
|
|
247
|
+
stdioTransport!.onclose = () => resolve();
|
|
248
|
+
stdioTransport!.onerror = (err: any) => reject(err);
|
|
249
|
+
});
|
|
250
|
+
} catch (err) {
|
|
251
|
+
console.error('Stdio: failed to start transport, falling back to HTTP:', err);
|
|
252
|
+
stdioTransport = null;
|
|
253
|
+
// restore console in case we fall back to HTTP server mode
|
|
254
|
+
console.log = _orig.log;
|
|
255
|
+
console.info = _orig.info;
|
|
256
|
+
console.warn = _orig.warn;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
228
259
|
const pendingPosts: Array<{ body: string; url: string | undefined; headers: any }> = [];
|
|
229
260
|
|
|
230
261
|
const { Readable, Writable } = await (async () => {
|