MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / start

Method start

src/mcp/client.ts:70–141  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

68 }
69
70 async start(): Promise<void> {
71 if (this.state !== 'stopped' && this.state !== 'failed') {
72 throw new Error(`MCP server ${this.name} is already ${this.state}`);
73 }
74 this.state = 'starting';
75 this.lastError = null;
76 this.tools = [];
77
78 this.transport = this.buildTransport();
79 this.transport.onMessage((msg) => this.handleMessage(msg));
80 this.transport.onError((err) => {
81 logger.warn(`MCP server ${this.name} transport error`, { err: err.message });
82 this.lastError = err.message;
83 });
84 this.transport.onClose((info) => {
85 logger.info(`MCP server ${this.name} closed`, info);
86 this.failAllPending(new Error(`MCP transport closed: ${info.reason ?? 'unknown'}`));
87 this.state = 'failed';
88 this.lastError = `closed: ${info.reason ?? 'unknown'}`;
89 this.emit('exit', info);
90 });
91
92 try {
93 logger.info(`Starting MCP server ${this.name} via ${this.transportName}`);
94 await this.transport.start();
95 } catch (e: any) {
96 this.state = 'failed';
97 this.lastError = `transport start failed: ${e.message}`;
98 throw new Error(this.lastError);
99 }
100
101 // Initialize handshake (with timeout)
102 const startupTimeout = (this.config.startupTimeoutSeconds ?? 10) * 1000;
103 try {
104 const initResult = await this.requestWithTimeout<MCPInitializeResult>(
105 'initialize',
106 {
107 protocolVersion: MCP_PROTOCOL_VERSION,
108 capabilities: { tools: {} },
109 clientInfo: { name: 'qodex', version: '0.3.0' },
110 },
111 startupTimeout,
112 );
113 this.capabilities = initResult.capabilities ?? {};
114 logger.info(`MCP ${this.name} initialized`, {
115 serverInfo: initResult.serverInfo,
116 capabilities: Object.keys(this.capabilities),
117 });
118
119 // Required notification per spec
120 this.notify('notifications/initialized');
121
122 // Pull tools list if supported
123 if (this.capabilities.tools !== undefined) {
124 const toolsResult = await this.requestWithTimeout<{ tools: MCPToolDef[] }>('tools/list', {}, startupTimeout);
125 this.tools = toolsResult.tools ?? [];
126 logger.info(`MCP ${this.name} loaded ${this.tools.length} tool(s)`, {
127 names: this.tools.map(t => t.name),

Callers 1

startOneMethod · 0.95

Calls 12

buildTransportMethod · 0.95
handleMessageMethod · 0.95
failAllPendingMethod · 0.95
notifyMethod · 0.95
onErrorMethod · 0.80
warnMethod · 0.80
onCloseMethod · 0.80
infoMethod · 0.80
onMessageMethod · 0.65
startMethod · 0.65
stopMethod · 0.65
emitMethod · 0.45

Tested by

no test coverage detected