Skip to content

Commit 7444b25

Browse files
authored
refactor(mcp): simplify HTTP transport and remove certificate error handling (#625)
1 parent 54d92b4 commit 7444b25

File tree

1 file changed

+62
-111
lines changed

1 file changed

+62
-111
lines changed

src/services/mcp/McpHub.ts

Lines changed: 62 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import * as path from "path"
1818
import * as vscode from "vscode"
1919
import { z } from "zod"
2020
import { t } from "../../i18n"
21-
import * as https from "https"
2221

2322
import { ClineProvider } from "../../core/webview/ClineProvider"
2423
import { GlobalFileNames } from "../../shared/globalFileNames"
@@ -620,20 +619,6 @@ export class McpHub {
620619
return state.mcpEnabled ?? true
621620
}
622621

623-
private shouldIgnoreCertificateErrors(): boolean {
624-
return process.env.IGNORE_MCP_SSL_CHECK === "1"
625-
}
626-
627-
private createIgnoreCertificateAgent(protocol?: string): https.Agent | undefined {
628-
if (protocol === "http:" || !this.shouldIgnoreCertificateErrors()) {
629-
return undefined
630-
}
631-
632-
return new https.Agent({
633-
rejectUnauthorized: false,
634-
})
635-
}
636-
637622
private async connectToServer(
638623
name: string,
639624
config: z.infer<typeof ServerConfigSchema>,
@@ -665,7 +650,7 @@ export class McpHub {
665650
try {
666651
const client = new Client(
667652
{
668-
name: "CoStrict",
653+
name: "Roo Code",
669654
version: this.providerRef.deref()?.context.extension?.packageJSON?.version ?? "1.0.0",
670655
},
671656
{
@@ -756,98 +741,74 @@ export class McpHub {
756741
} else {
757742
console.error(`No stderr stream for ${name}`)
758743
}
759-
} else if (["streamable-http", "sse"].includes(configInjected.type)) {
760-
const _fetch = (url: string | URL, init?: RequestInit) => {
761-
const headers = new Headers({ ...(init?.headers || {}), ...(configInjected.headers || {}) })
762-
let protocol = ""
763-
const fetchOptions: RequestInit = {
764-
...init,
765-
headers,
766-
}
767-
if (url instanceof URL) {
768-
protocol = url.protocol
769-
} else if (typeof url === "string") {
770-
protocol = url.startsWith("https:") ? "https:" : "http:"
771-
}
772-
773-
const ignoreCertAgent = this.createIgnoreCertificateAgent(protocol)
774-
if (ignoreCertAgent) {
775-
;(fetchOptions as any).agent = ignoreCertAgent
776-
}
777-
778-
return fetch(url, fetchOptions)
779-
}
780-
781-
if (configInjected.type === "streamable-http") {
782-
// Streamable HTTP connection
783-
const requestInit: RequestInit = {
744+
} else if (configInjected.type === "streamable-http") {
745+
// Streamable HTTP connection
746+
transport = new StreamableHTTPClientTransport(new URL(configInjected.url), {
747+
requestInit: {
784748
headers: configInjected.headers,
785-
}
786-
787-
transport = new StreamableHTTPClientTransport(new URL(configInjected.url), {
788-
requestInit,
789-
fetch: _fetch,
790-
})
749+
},
750+
})
791751

792-
// Set up Streamable HTTP specific error handling
793-
transport.onerror = async (error) => {
794-
console.error(`Transport error for "${name}" (streamable-http):`, error)
795-
const connection = this.findConnection(name, source)
796-
if (connection) {
797-
connection.server.status = "disconnected"
798-
this.appendErrorMessage(connection, error instanceof Error ? error.message : `${error}`)
799-
}
800-
await this.notifyWebviewOfServerChanges()
752+
// Set up Streamable HTTP specific error handling
753+
transport.onerror = async (error) => {
754+
console.error(`Transport error for "${name}" (streamable-http):`, error)
755+
const connection = this.findConnection(name, source)
756+
if (connection) {
757+
connection.server.status = "disconnected"
758+
this.appendErrorMessage(connection, error instanceof Error ? error.message : `${error}`)
801759
}
760+
await this.notifyWebviewOfServerChanges()
761+
}
802762

803-
transport.onclose = async () => {
804-
const connection = this.findConnection(name, source)
805-
if (connection) {
806-
connection.server.status = "disconnected"
807-
}
808-
await this.notifyWebviewOfServerChanges()
809-
}
810-
} else if (configInjected.type === "sse") {
811-
// SSE connection
812-
const sseOptions = {
813-
requestInit: {
814-
headers: configInjected.headers,
815-
},
816-
}
817-
// Configure ReconnectingEventSource options
818-
const reconnectingEventSourceOptions = {
819-
max_retry_time: 5000, // Maximum retry time in milliseconds
820-
withCredentials: configInjected.headers?.["Authorization"] ? true : false, // Enable credentials if Authorization header exists
821-
fetch: _fetch,
763+
transport.onclose = async () => {
764+
const connection = this.findConnection(name, source)
765+
if (connection) {
766+
connection.server.status = "disconnected"
822767
}
823-
global.EventSource = ReconnectingEventSource
824-
825-
transport = new SSEClientTransport(new URL(configInjected.url), {
826-
...sseOptions,
827-
eventSourceInit: reconnectingEventSourceOptions,
828-
})
768+
await this.notifyWebviewOfServerChanges()
769+
}
770+
} else if (configInjected.type === "sse") {
771+
// SSE connection
772+
const sseOptions = {
773+
requestInit: {
774+
headers: configInjected.headers,
775+
},
776+
}
777+
// Configure ReconnectingEventSource options
778+
const reconnectingEventSourceOptions = {
779+
max_retry_time: 5000, // Maximum retry time in milliseconds
780+
withCredentials: configInjected.headers?.["Authorization"] ? true : false, // Enable credentials if Authorization header exists
781+
fetch: (url: string | URL, init: RequestInit) => {
782+
const headers = new Headers({ ...(init?.headers || {}), ...(configInjected.headers || {}) })
783+
return fetch(url, {
784+
...init,
785+
headers,
786+
})
787+
},
788+
}
789+
global.EventSource = ReconnectingEventSource
790+
transport = new SSEClientTransport(new URL(configInjected.url), {
791+
...sseOptions,
792+
eventSourceInit: reconnectingEventSourceOptions,
793+
})
829794

830-
// Set up SSE specific error handling
831-
transport.onerror = async (error) => {
832-
console.error(`Transport error for "${name}":`, error)
833-
const connection = this.findConnection(name, source)
834-
if (connection) {
835-
connection.server.status = "disconnected"
836-
this.appendErrorMessage(connection, error instanceof Error ? error.message : `${error}`)
837-
}
838-
await this.notifyWebviewOfServerChanges()
795+
// Set up SSE specific error handling
796+
transport.onerror = async (error) => {
797+
console.error(`Transport error for "${name}":`, error)
798+
const connection = this.findConnection(name, source)
799+
if (connection) {
800+
connection.server.status = "disconnected"
801+
this.appendErrorMessage(connection, error instanceof Error ? error.message : `${error}`)
839802
}
803+
await this.notifyWebviewOfServerChanges()
804+
}
840805

841-
transport.onclose = async () => {
842-
const connection = this.findConnection(name, source)
843-
if (connection) {
844-
connection.server.status = "disconnected"
845-
}
846-
await this.notifyWebviewOfServerChanges()
806+
transport.onclose = async () => {
807+
const connection = this.findConnection(name, source)
808+
if (connection) {
809+
connection.server.status = "disconnected"
847810
}
848-
} else {
849-
// Should not happen if validateServerConfig is correct
850-
throw new Error(`Unsupported MCP server type: ${(configInjected as any).type}`)
811+
await this.notifyWebviewOfServerChanges()
851812
}
852813
} else {
853814
// Should not happen if validateServerConfig is correct
@@ -1715,7 +1676,7 @@ export class McpHub {
17151676
timeout = 60 * 1000
17161677
}
17171678

1718-
const result = await connection.client.request(
1679+
return (await connection.client.request(
17191680
{
17201681
method: "tools/call",
17211682
params: {
@@ -1727,17 +1688,7 @@ export class McpHub {
17271688
{
17281689
timeout,
17291690
},
1730-
)
1731-
1732-
if (result && typeof result === "object" && "result" in result) {
1733-
return result as McpToolCallResponse
1734-
}
1735-
1736-
return {
1737-
_meta: result._meta,
1738-
content: result.content as McpToolCallResponse["content"],
1739-
isError: result.isError,
1740-
}
1691+
)) as McpToolCallResponse
17411692
}
17421693

17431694
/**

0 commit comments

Comments
 (0)