-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathserver.ts
More file actions
88 lines (70 loc) · 2.84 KB
/
server.ts
File metadata and controls
88 lines (70 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { IBClient } from "./ib-client.js";
import { IBGatewayManager } from "./gateway-manager.js";
import { config } from "./config.js";
import { registerTools } from "./tools.js";
import { Logger } from "./logger.js";
export const configSchema = z.object({
// Authentication configuration
IB_USERNAME: z.string().optional(),
IB_PASSWORD_AUTH: z.string().optional(),
IB_AUTH_TIMEOUT: z.number().optional(),
IB_HEADLESS_MODE: z.boolean().optional(),
// Paper trading configuration
IB_PAPER_TRADING: z.boolean().optional(),
});
// Global gateway manager instance
let gatewayManager: IBGatewayManager | null = null;
// Initialize and start IB Gateway (fast startup for MCP plugin compatibility)
async function initializeGateway(ibClient?: IBClient) {
if (!gatewayManager) {
gatewayManager = new IBGatewayManager();
try {
Logger.info('⚡ Quick Gateway initialization for MCP plugin...');
await gatewayManager.quickStartGateway();
Logger.info('✅ Gateway initialization completed (background startup if needed)');
// Update client port if provided
if (ibClient) {
ibClient.updatePort(gatewayManager.getCurrentPort());
}
} catch (error) {
Logger.error('❌ Failed to initialize Gateway:', error);
// Don't throw error during quick startup - tools will handle it
Logger.warn('⚠️ Gateway initialization failed, tools will attempt connection when called');
}
}
return gatewayManager;
}
export function createIBMCPServer({ config: userConfig }: { config: z.infer<typeof configSchema> }) {
// Merge user config with environment config
const mergedConfig = {
...config,
...userConfig
};
// Log the merged config for debugging (but redact sensitive info)
const logConfig = { ...mergedConfig };
if (logConfig.IB_PASSWORD_AUTH) logConfig.IB_PASSWORD_AUTH = '[REDACTED]';
if (logConfig.IB_PASSWORD) logConfig.IB_PASSWORD = '[REDACTED]';
Logger.info(`🔍 Final merged config: ${JSON.stringify(logConfig, null, 2)}`);
// Create IB Client with default port initially - this will be updated once gateway starts
const ibClient = new IBClient({
host: mergedConfig.IB_GATEWAY_HOST,
port: mergedConfig.IB_GATEWAY_PORT,
});
// Initialize gateway on first server creation and update client port
initializeGateway(ibClient).catch(error => {
Logger.error('Failed to initialize gateway:', error);
});
Logger.info('Gateway starting...');
// Create MCP server
const server = new McpServer({
name: "interactive-brokers-mcp",
version: "1.0.0"
});
// Register all tools with merged config
registerTools(server, ibClient, gatewayManager || undefined, mergedConfig);
Logger.info('Tools registered');
return server;
}
export { gatewayManager };