修复无法重新连接的问题

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
await loadPanels();
console.log(route);
if (route.name !== 'debug') {
router.replace('/debug');
router.push('/debug');

View File

@ -1,6 +1,7 @@
import { useMessageBridge } from '@/api/message-bridge';
import { reactive } from 'vue';
import { pinkLog } from '../setting/util';
import { ElMessage } from 'element-plus';
export const connectionMethods = reactive({
current: 'STDIO',
@ -66,9 +67,17 @@ export function doConnect() {
connectionResult.success = (code === 200);
connectionResult.logString = msg;
if (code === 200) {
const res = await getServerVersion() as { name: string, version: string };
connectionResult.serverInfo.name = res.name || '';
connectionResult.serverInfo.version = res.version || '';
} else {
ElMessage({
type: 'error',
message: msg
});
}
resolve(void 0);
}, { 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 { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
import { Implementation } from "@modelcontextprotocol/sdk/types";
import { Writable, Stream } from "node:stream";
// 定义连接类型
type ConnectionType = 'STDIO' | 'SSE';
@ -31,6 +32,8 @@ export class MCPClient {
private options: MCPOptions;
private serverVersion: IServerVersion;
private transportStdErr: string = '';
constructor(options: MCPOptions) {
this.options = options;
this.serverVersion = undefined;
@ -52,29 +55,49 @@ export class MCPClient {
// 连接方法
public async connect(): Promise<void> {
this.transportStdErr = '';
// 根据连接类型创建传输层
switch (this.options.connectionType) {
case 'STDIO':
this.transport = new StdioClientTransport({
command: this.options.command || '',
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;
case 'SSE':
if (!this.options.url) {
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;
default:
throw new Error(`Unsupported connection type: ${this.options.connectionType}`);
}
// 建立连接
if (this.transport) {
await this.client.connect(this.transport);
console.log(`Connected to MCP server via ${this.options.connectionType}`);
}
}
public getServerVersion() {
if (this.serverVersion) {

View File

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