Skip to content

Commit 9290990

Browse files
committed
Merge branch 'main' into refactor-mcp && optimize ConnectionHandler
1 parent c4c3767 commit 9290990

File tree

12 files changed

+212
-328
lines changed

12 files changed

+212
-328
lines changed

src/core/webview/webviewMessageHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ export const webviewMessageHandler = async (provider: ClineProvider, message: We
393393
break
394394
}
395395
case "openMcpSettings": {
396-
const mcpSettingsFilePath = await provider.getMcpHub()?.getMcpSettingsFilePath()
396+
const mcpSettingsFilePath = await provider.getMcpHub()?.getGlobalMcpSettingsFilePath()
397397

398398
if (mcpSettingsFilePath) {
399399
openFile(mcpSettingsFilePath)

src/services/mcp/McpHub.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { ClineProvider } from "../../core/webview/ClineProvider"
33
import type { ConfigChangeEvent } from "./config"
44
import { ConfigManager } from "./config"
55
import { ConnectionFactory, ConnectionManager, SseHandler, StdioHandler } from "./connection"
6-
import { ConfigSource, McpConnection, McpResourceResponse, McpServer, McpToolCallResponse, ServerConfig } from "./types"
6+
import { McpConnection, McpResourceResponse, McpToolCallResponse, ServerConfig } from "./types"
7+
import { ConfigSource, McpServer } from "../../shared/mcp"
78

89
export class McpHub {
910
private configManager: ConfigManager

src/services/mcp/config/ConfigManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import { t } from "../../../i18n"
77
import { ClineProvider } from "../../../core/webview/ClineProvider"
88
import { GlobalFileNames } from "../../../shared/globalFileNames"
99
import { fileExistsAtPath } from "../../../utils/fs"
10-
import { ServerConfig, McpServer, ConfigSource } from "../types"
10+
import { ServerConfig } from "../types"
1111
import { ConfigChangeEvent, ConfigChangeListener } from "./types"
1212
import { safeParseServerConfig } from "./validation"
13+
import { ConfigSource, McpServer } from "../../../shared/mcp"
1314

1415
/**
1516
* Configuration Manager

src/services/mcp/config/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ConfigSource, ServerConfig } from "../types"
1+
import { ServerConfig } from "../types"
2+
import { ConfigSource } from "../../../shared/mcp"
23

34
/**
45
* Configuration change event

src/services/mcp/connection/ConnectionFactory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { ServerConfig, McpConnection, McpServer, ConfigSource } from "../types"
1+
import { ServerConfig, McpConnection } from "../types"
22
import { ConnectionHandler } from "./ConnectionHandler"
33
import { FileWatcher } from "./FileWatcher"
44
import { ConfigManager } from "../config"
5+
import { ConfigSource, McpServer } from "../../../shared/mcp"
56

67
/**
78
* Connection factory class

src/services/mcp/connection/ConnectionHandler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ServerConfig, McpConnection, McpServer, ConfigSource } from "../types"
1+
import { ServerConfig, McpConnection } from "../types"
2+
import { ConfigSource, McpServer } from "../../../shared/mcp"
23

34
/**
45
* Connection handler interface

src/services/mcp/connection/ConnectionManager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as vscode from "vscode"
22
import { t } from "../../../i18n"
3-
import { ConfigManager } from "../config/ConfigManager"
4-
import { ServerConfig, McpConnection, McpServer, ConfigSource } from "../types"
3+
import { ConfigManager } from "../config"
4+
import { ServerConfig, McpConnection } from "../types"
55
import { ConnectionFactory } from "./ConnectionFactory"
66
import deepEqual from "fast-deep-equal"
7+
import { ConfigSource, McpServer } from "../../../shared/mcp"
78

89
/**
910
* Connection manager class
Lines changed: 8 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
1-
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
2-
const packageJson = require("../../../../../package.json")
3-
const version: string = packageJson.version ?? "1.0.0"
41
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js"
5-
import {
6-
ListToolsResultSchema,
7-
ListResourcesResultSchema,
8-
ListResourceTemplatesResultSchema,
9-
} from "@modelcontextprotocol/sdk/types.js"
10-
import { ConnectionHandler } from "../ConnectionHandler"
11-
import {
12-
ServerConfig,
13-
McpConnection,
14-
ConfigSource,
15-
McpTool,
16-
McpResource,
17-
McpResourceTemplate,
18-
McpServer,
19-
} from "../../types"
2+
import { ServerConfig, McpConnection } from "../../types"
3+
import { ConfigSource, McpServer } from "../../../../shared/mcp"
4+
import { BaseHandler } from "./base/BaseHandler"
205

216
/**
227
* SSE connection handler
238
* Responsible for creating and managing MCP connections based on Server-Sent Events
249
*/
25-
export class SseHandler implements ConnectionHandler {
10+
export class SseHandler extends BaseHandler {
2611
/**
2712
* Check if a specific connection type is supported
2813
* @param type Connection type
@@ -51,15 +36,7 @@ export class SseHandler implements ConnectionHandler {
5136
}
5237

5338
// Create client
54-
const client = new Client(
55-
{
56-
name: "Roo Code",
57-
version,
58-
},
59-
{
60-
capabilities: {},
61-
},
62-
)
39+
const client = this.createClient()
6340

6441
// Create transport
6542
const transport = new SSEClientTransport(new URL(config.url), {
@@ -79,6 +56,7 @@ export class SseHandler implements ConnectionHandler {
7956
status: "connecting",
8057
disabled: config.disabled,
8158
source,
59+
errorHistory: [],
8260
},
8361
client,
8462
transport,
@@ -100,38 +78,20 @@ export class SseHandler implements ConnectionHandler {
10078
connection.server.resourceTemplates = await this.fetchResourceTemplatesList(connection)
10179
} catch (error) {
10280
connection.server.status = "disconnected"
103-
connection.server.error = error instanceof Error ? error.message : `${error}`
81+
this.appendErrorMessage(connection, error instanceof Error ? error.message : `${error}`)
10482
if (onStatusChange) onStatusChange(connection.server)
10583
}
10684

10785
return connection
10886
}
10987

110-
/**
111-
* Close connection
112-
* @param connection Connection to close
113-
*/
114-
async closeConnection(connection: McpConnection): Promise<void> {
115-
try {
116-
await connection.client.close()
117-
} catch (error) {
118-
console.error(`Error disconnecting client for ${connection.server.name}:`, error)
119-
}
120-
121-
try {
122-
await connection.transport.close()
123-
} catch (error) {
124-
console.error(`Error closing transport for ${connection.server.name}:`, error)
125-
}
126-
}
127-
12888
/**
12989
* Setup error handling
13090
* @param connection MCP connection
13191
* @param transport SSE transport
13292
* @param onStatusChange
13393
*/
134-
private setupErrorHandling(
94+
protected setupErrorHandling(
13595
connection: McpConnection,
13696
transport: SSEClientTransport,
13797
onStatusChange?: (server: McpServer) => void,
@@ -151,70 +111,4 @@ export class SseHandler implements ConnectionHandler {
151111
if (onStatusChange) onStatusChange(connection.server)
152112
}
153113
}
154-
155-
/**
156-
* Fetch tool list
157-
* @param connection MCP connection
158-
* @returns Tool list
159-
*/
160-
private async fetchToolsList(connection: McpConnection): Promise<McpTool[]> {
161-
try {
162-
const result = await connection.client.listTools()
163-
const parsed = ListToolsResultSchema.parse(result)
164-
165-
return parsed.tools.map((tool: any) => ({
166-
name: tool.name,
167-
description: tool.description,
168-
inputSchema: tool.input_schema as object | undefined,
169-
alwaysAllow: false,
170-
}))
171-
} catch (error) {
172-
// console.error(`Failed to fetch tools list for ${connection.server.name}:`, error)
173-
return []
174-
}
175-
}
176-
177-
/**
178-
* Fetch resource list
179-
* @param connection MCP connection
180-
* @returns Resource list
181-
*/
182-
private async fetchResourcesList(connection: McpConnection): Promise<McpResource[]> {
183-
try {
184-
const result = await connection.client.listResources()
185-
const parsed = ListResourcesResultSchema.parse(result)
186-
187-
return parsed.resources.map((resource: any) => ({
188-
uri: resource.uri,
189-
name: resource.name,
190-
mimeType: resource.mime_type as string | undefined,
191-
description: resource.description,
192-
}))
193-
} catch (error) {
194-
// console.error(`Failed to fetch resources list for ${connection.server.name}:`, error)
195-
return []
196-
}
197-
}
198-
199-
/**
200-
* Fetch resource template list
201-
* @param connection MCP connection
202-
* @returns Resource template list
203-
*/
204-
private async fetchResourceTemplatesList(connection: McpConnection): Promise<McpResourceTemplate[]> {
205-
try {
206-
const result = await connection.client.listResourceTemplates()
207-
const parsed = ListResourceTemplatesResultSchema.parse(result)
208-
209-
return (parsed as any).templates.map((template: any) => ({
210-
uri: template.uri,
211-
name: template.name,
212-
description: template.description,
213-
inputSchema: template.input_schema as object | undefined,
214-
}))
215-
} catch (error) {
216-
// console.error(`Failed to fetch resource templates list for ${connection.server.name}:`, error)
217-
return []
218-
}
219-
}
220114
}

0 commit comments

Comments
 (0)