fix: enhance MCP server tool registration and client initialization logic (#31)

This commit is contained in:
samanhappy
2025-04-23 16:27:21 +08:00
committed by GitHub
parent c1eabb5607
commit bbb99b6f17
2 changed files with 15 additions and 9 deletions

View File

@@ -7,6 +7,7 @@ dotenv.config();
const defaultConfig = {
port: process.env.PORT || 3000,
initTimeout: process.env.INIT_TIMEOUT || 300000,
timeout: process.env.REQUEST_TIMEOUT || 60000,
mcpHubName: 'mcphub',
mcpHubVersion: '0.0.1',
@@ -42,4 +43,4 @@ export const expandEnvVars = (value: string): string => {
return value.replace(/\$\{([^}]+)\}/g, (_, key) => process.env[key] || '');
};
export default defaultConfig;
export default defaultConfig;

View File

@@ -13,7 +13,7 @@ let currentServer: Server;
export const initMcpServer = async (name: string, version: string): Promise<void> => {
currentServer = createMcpServer(name, version);
await registerAllTools(currentServer, true);
await registerAllTools(currentServer, true, true);
};
export const setMcpServer = (server: Server): void => {
@@ -25,7 +25,7 @@ export const getMcpServer = (): Server => {
};
export const notifyToolChanged = async () => {
await registerAllTools(currentServer, true);
await registerAllTools(currentServer, true, false);
currentServer
.sendToolListChanged()
.catch((error) => {
@@ -40,7 +40,7 @@ export const notifyToolChanged = async () => {
let serverInfos: ServerInfo[] = [];
// Initialize MCP server clients
export const initializeClientsFromSettings = (): ServerInfo[] => {
export const initializeClientsFromSettings = (isInit: boolean): ServerInfo[] => {
const settings = loadSettings();
const existingServerInfos = serverInfos;
serverInfos = [];
@@ -109,13 +109,14 @@ export const initializeClientsFromSettings = (): ServerInfo[] => {
},
},
);
const timeout = isInit ? Number(config.initTimeout) : Number(config.timeout);
client
.connect(transport, { timeout: Number(config.timeout) })
.connect(transport, { timeout: timeout })
.then(() => {
console.log(`Successfully connected client for server: ${name}`);
client
.listTools({}, { timeout: Number(config.timeout) })
.listTools({}, { timeout: timeout })
.then((tools) => {
console.log(`Successfully listed ${tools.tools.length} tools for server: ${name}`);
const serverInfo = getServerByName(name);
@@ -169,8 +170,12 @@ export const initializeClientsFromSettings = (): ServerInfo[] => {
};
// Register all MCP tools
export const registerAllTools = async (server: Server, forceInit: boolean): Promise<void> => {
initializeClientsFromSettings();
export const registerAllTools = async (
server: Server,
forceInit: boolean,
isInit: boolean,
): Promise<void> => {
initializeClientsFromSettings(isInit);
};
// Get all server information
@@ -221,7 +226,7 @@ export const addServer = async (
return { success: false, message: 'Failed to save settings' };
}
registerAllTools(currentServer, false);
registerAllTools(currentServer, false, false);
return { success: true, message: 'Server added successfully' };
} catch (error) {
console.error(`Failed to add server: ${name}`, error);