Created
February 10, 2026 02:40
-
-
Save xeioex/569699985c981bd0b812a940b8fd696f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var _mcp_messages = []; | |
| var _mcp_buffer = ""; | |
| function clear_content_length(r) { | |
| delete r.headersOut['Content-Length']; | |
| } | |
| function filter(r, data, flags) { | |
| _mcp_buffer += data; | |
| r.sendBuffer(data, flags); | |
| if (_mcp_messages.length === 0) { | |
| var sse_messages = _mcp_buffer.split(/\\n\\n/); | |
| for (var i = 0; i < sse_messages.length; i++) { | |
| var msg = sse_messages[i]; | |
| r.log("Processing SSE message: " + msg); | |
| if (msg.startsWith("data: ")) { | |
| var json_str = msg.substring(6); // "data: ".length | |
| try { | |
| var json_obj = JSON.parse(json_str); | |
| _mcp_messages.push(json_obj); | |
| r.done(); | |
| return; | |
| } catch (e) { | |
| } | |
| } | |
| } | |
| } | |
| } | |
| function getPath(r, json_obj, path) { | |
| if (!json_obj) { | |
| return undefined; | |
| } | |
| var parts = path.split('.'); | |
| var current = json_obj; | |
| for (var i = 0; i < parts.length; i++) { | |
| var part = parts[i]; | |
| if (typeof current !== 'object' || current === null || !current.hasOwnProperty(part)) { | |
| return undefined; | |
| } | |
| current = current[part]; | |
| } | |
| return current; | |
| } | |
| function has_error(r) { | |
| if (_mcp_messages.length === 0) { | |
| r.log("No MCP messages parsed yet, cannot determine error status"); | |
| return false; // No message parsed yet, assume no error | |
| } | |
| var first_message = _mcp_messages[0]; | |
| r.log("Checking for errors in message: " + JSON.stringify(first_message)); | |
| // JSON-RPC2 error response | |
| if (getPath(r, first_message, "error")) { | |
| r.log("Error found in JSON-RPC2 response"); | |
| return true; | |
| } | |
| // Tool error (IsError: true in result) | |
| if (getPath(r, first_message, "result.isError")) { | |
| r.log("Error found in tool execution result"); | |
| return true; | |
| } | |
| return false; | |
| } | |
| function mcp_tool_name(r) { | |
| var body = JSON.parse(r.requestText || '{}'); | |
| var method = body.method; | |
| if (method == 'tools/call') { | |
| return body.params.name; | |
| } | |
| return ''; | |
| } | |
| function mcp_tool_status(r) { | |
| if (has_error(r)) { | |
| return 'error'; | |
| } | |
| return 'ok'; | |
| } | |
| export default { filter, clear_content_length, mcp_tool_name, mcp_tool_status }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment