MCPcopy Create free account
hub / github.com/universal-tool-calling-protocol/code-mode / initializeUtcpClient

Function initializeUtcpClient

code-mode-mcp/index.ts:215–266  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

213}
214
215async function initializeUtcpClient(): Promise<CodeModeUtcpClient> {
216 if (utcpClient) {
217 return utcpClient;
218 }
219
220 // Look for config file: 1) Environment variable, 2) Current working directory, 3) Package directory
221 const cwd = process.cwd();
222 const packageDir = __dirname;
223
224 let configPath: string;
225 let scriptDir: string;
226
227 // Check if UTCP_CONFIG_FILE environment variable is set
228 if (process.env.UTCP_CONFIG_FILE) {
229 configPath = path.resolve(process.env.UTCP_CONFIG_FILE);
230 scriptDir = path.dirname(configPath);
231
232 try {
233 await fs.access(configPath);
234 } catch {
235 console.warn(`UTCP config file specified in UTCP_CONFIG_FILE not found: ${configPath}`);
236 }
237 } else {
238 // Fall back to current working directory first, then package directory
239 configPath = path.resolve(cwd, '.utcp_config.json');
240 scriptDir = cwd;
241
242 try {
243 await fs.access(configPath);
244 } catch {
245 configPath = path.resolve(packageDir, '.utcp_config.json');
246 scriptDir = packageDir;
247 }
248 }
249
250 let rawConfig: any = {};
251 try {
252 const configFileContent = await fs.readFile(configPath, 'utf-8');
253 rawConfig = JSON.parse(configFileContent);
254 } catch (e: any) {
255 if (e.code !== 'ENOENT') {
256 console.warn(`Could not read or parse .utcp_config.json. Error: ${e.message}`);
257 }
258 }
259
260 const clientConfig = new UtcpClientConfigSerializer().validateDict(rawConfig);
261
262 const newClient = await CodeModeUtcpClient.create(scriptDir, clientConfig);
263
264 utcpClient = newClient;
265 return utcpClient;
266}
267
268main().catch(err => {
269 console.error("Failed to start UTCP-MCP Bridge:", err);

Callers 2

mainFunction · 0.85
setupMcpToolsFunction · 0.85

Calls 1

createMethod · 0.45

Tested by

no test coverage detected