forked from modelcontextprotocol/typescript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Transport } from "../shared/transport.js"; | ||
import { JSONRPCMessage, JSONRPCMessageSchema } from "../types.js"; | ||
|
||
|
||
export class HTTPClientTransport implements Transport { | ||
private _url: URL; | ||
private _requestInit?: RequestInit; | ||
private _abortController?: AbortController; | ||
|
||
onclose?: () => void; | ||
onerror?: (error: Error) => void; | ||
onmessage?: (message: JSONRPCMessage) => void; | ||
|
||
constructor(url: URL, requestInit?: RequestInit) { | ||
this._url = url; | ||
this._requestInit = requestInit; | ||
} | ||
|
||
async start(): Promise<void> { | ||
this._abortController = new AbortController(); | ||
} | ||
|
||
async send(message: JSONRPCMessage): Promise<void> { | ||
try { | ||
const headers = new Headers(this._requestInit?.headers); | ||
headers.set("Content-Type", "application/json"); | ||
const init: RequestInit = { | ||
...this._requestInit, | ||
method: "POST", | ||
body: JSON.stringify(message), | ||
signal: this._abortController?.signal, | ||
}; | ||
const response = await fetch(this._url, init); | ||
|
||
if (!response.ok) { | ||
const text = await response.text().catch(() => null); | ||
throw new Error(`Error POSTing to URL (HTTP ${response.status}): ${text}`); | ||
} | ||
|
||
let responseMessage: JSONRPCMessage; | ||
const responseJson = await response.json(); | ||
responseMessage = JSONRPCMessageSchema.parse(responseJson); | ||
|
||
this.onmessage?.(responseMessage); | ||
} catch (error) { | ||
this.onerror?.(error as Error); | ||
return; | ||
} | ||
} | ||
|
||
async close(): Promise<void> { | ||
this._abortController?.abort(); | ||
this.onclose?.(); | ||
} | ||
} |