修复无法重新连接的问题

This commit is contained in:
锦恢 2025-04-20 02:07:01 +08:00
parent 3e0622f7e7
commit 5c9f46d2f1
4 changed files with 40 additions and 18 deletions

View File

@ -62,8 +62,6 @@ async function initProduce() {
// tab // tab
await loadPanels(); await loadPanels();
console.log(route);
if (route.name !== 'debug') { if (route.name !== 'debug') {
router.replace('/debug'); router.replace('/debug');
router.push('/debug'); router.push('/debug');

View File

@ -1,6 +1,7 @@
import { useMessageBridge } from '@/api/message-bridge'; import { useMessageBridge } from '@/api/message-bridge';
import { reactive } from 'vue'; import { reactive } from 'vue';
import { pinkLog } from '../setting/util'; import { pinkLog } from '../setting/util';
import { ElMessage } from 'element-plus';
export const connectionMethods = reactive({ export const connectionMethods = reactive({
current: 'STDIO', current: 'STDIO',
@ -66,9 +67,17 @@ export function doConnect() {
connectionResult.success = (code === 200); connectionResult.success = (code === 200);
connectionResult.logString = msg; connectionResult.logString = msg;
if (code === 200) {
const res = await getServerVersion() as { name: string, version: string }; const res = await getServerVersion() as { name: string, version: string };
connectionResult.serverInfo.name = res.name || ''; connectionResult.serverInfo.name = res.name || '';
connectionResult.serverInfo.version = res.version || ''; connectionResult.serverInfo.version = res.version || '';
} else {
ElMessage({
type: 'error',
message: msg
});
}
resolve(void 0); resolve(void 0);
}, { once: true }); }, { once: true });

View File

@ -3,6 +3,7 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"; import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js"; import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
import { Implementation } from "@modelcontextprotocol/sdk/types"; import { Implementation } from "@modelcontextprotocol/sdk/types";
import { Writable, Stream } from "node:stream";
// 定义连接类型 // 定义连接类型
type ConnectionType = 'STDIO' | 'SSE'; type ConnectionType = 'STDIO' | 'SSE';
@ -31,6 +32,8 @@ export class MCPClient {
private options: MCPOptions; private options: MCPOptions;
private serverVersion: IServerVersion; private serverVersion: IServerVersion;
private transportStdErr: string = '';
constructor(options: MCPOptions) { constructor(options: MCPOptions) {
this.options = options; this.options = options;
this.serverVersion = undefined; this.serverVersion = undefined;
@ -52,29 +55,49 @@ export class MCPClient {
// 连接方法 // 连接方法
public async connect(): Promise<void> { public async connect(): Promise<void> {
this.transportStdErr = '';
// 根据连接类型创建传输层 // 根据连接类型创建传输层
switch (this.options.connectionType) { switch (this.options.connectionType) {
case 'STDIO': case 'STDIO':
this.transport = new StdioClientTransport({ this.transport = new StdioClientTransport({
command: this.options.command || '', command: this.options.command || '',
args: this.options.args || [], args: this.options.args || [],
cwd: this.options.cwd || process.cwd() cwd: this.options.cwd || process.cwd(),
// TODO
stderr: 'pipe'
}); });
this.transport.onmessage = (message) => {
console.log('Received message from server:', message);
this.transportStdErr += message;
};
this.transport.onerror = (error) => {
console.log('Error from server:', error);
this.transportStdErr += error;
};
break; break;
case 'SSE': case 'SSE':
if (!this.options.url) { if (!this.options.url) {
throw new Error('URL is required for SSE connection'); throw new Error('URL is required for SSE connection');
} }
this.transport = new SSEClientTransport(new URL(this.options.url)); this.transport = new SSEClientTransport(
new URL(this.options.url)
);
break; break;
default: default:
throw new Error(`Unsupported connection type: ${this.options.connectionType}`); throw new Error(`Unsupported connection type: ${this.options.connectionType}`);
} }
// 建立连接 // 建立连接
if (this.transport) {
await this.client.connect(this.transport); await this.client.connect(this.transport);
console.log(`Connected to MCP server via ${this.options.connectionType}`); console.log(`Connected to MCP server via ${this.options.connectionType}`);
} }
}
public getServerVersion() { public getServerVersion() {
if (this.serverVersion) { if (this.serverVersion) {

View File

@ -1,12 +1,4 @@
{ {
"currentIndex": 0, "currentIndex": 0,
"tabs": [ "tabs": []
{
"name": "Blank test 1",
"icon": "icon-blank",
"type": "blank",
"componentIndex": -1,
"storage": {}
}
]
} }