Skip to content

Commit 52872c5

Browse files
committed
feat: add PoC of HTTP client transport for TypeScript
This client transport is a stateless transport that uses JSONRPC over HTTP POST, foregoing stateful communication and therefore avoiding some of the complexity of the SSE transport (and the need for persistent connections). It does mean that server-to-client communication is not possible. This transport is also implemented in Python in [this PR], and the TypeScript client has been tested against the Python server. [this PR]: grafana/mcp-grafana#24
1 parent 423b62b commit 52872c5

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/cli.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import WebSocket from "ws";
55

66
import express from "express";
77
import { Client } from "./client/index.js";
8+
import { HTTPClientTransport } from "./client/http.js";
89
import { SSEClientTransport } from "./client/sse.js";
910
import { StdioClientTransport } from "./client/stdio.js";
1011
import { WebSocketClientTransport } from "./client/websocket.js";
@@ -36,7 +37,10 @@ async function runClient(url_or_command: string, args: string[]) {
3637
}
3738

3839
if (url?.protocol === "http:" || url?.protocol === "https:") {
39-
clientTransport = new SSEClientTransport(new URL(url_or_command));
40+
clientTransport = new HTTPClientTransport(new URL(url_or_command));
41+
} else if (url?.protocol === "http+sse:" || url?.protocol === "https+sse:" || url?.protocol === "wss:") {
42+
url = new URL(url.toString().replace("+sse", ""));
43+
clientTransport = new SSEClientTransport(url);
4044
} else if (url?.protocol === "ws:" || url?.protocol === "wss:") {
4145
clientTransport = new WebSocketClientTransport(new URL(url_or_command));
4246
} else {

src/client/http.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Transport } from "../shared/transport.js";
2+
import { JSONRPCMessage, JSONRPCMessageSchema } from "../types.js";
3+
4+
5+
export class HTTPClientTransport implements Transport {
6+
private _url: URL;
7+
private _requestInit?: RequestInit;
8+
private _abortController?: AbortController;
9+
10+
onclose?: () => void;
11+
onerror?: (error: Error) => void;
12+
onmessage?: (message: JSONRPCMessage) => void;
13+
14+
constructor(url: URL, requestInit?: RequestInit) {
15+
this._url = url;
16+
this._requestInit = requestInit;
17+
}
18+
19+
async start(): Promise<void> {
20+
this._abortController = new AbortController();
21+
}
22+
23+
async send(message: JSONRPCMessage): Promise<void> {
24+
try {
25+
const headers = new Headers(this._requestInit?.headers);
26+
headers.set("Content-Type", "application/json");
27+
const init: RequestInit = {
28+
...this._requestInit,
29+
method: "POST",
30+
body: JSON.stringify(message),
31+
signal: this._abortController?.signal,
32+
};
33+
const response = await fetch(this._url, init);
34+
35+
if (!response.ok) {
36+
const text = await response.text().catch(() => null);
37+
throw new Error(`Error POSTing to URL (HTTP ${response.status}): ${text}`);
38+
}
39+
40+
let responseMessage: JSONRPCMessage;
41+
const responseJson = await response.json();
42+
responseMessage = JSONRPCMessageSchema.parse(responseJson);
43+
44+
this.onmessage?.(responseMessage);
45+
} catch (error) {
46+
this.onerror?.(error as Error);
47+
return;
48+
}
49+
}
50+
51+
async close(): Promise<void> {
52+
this._abortController?.abort();
53+
this.onclose?.();
54+
}
55+
}

0 commit comments

Comments
 (0)