You can execute TypeScript code that has direct access to UTCP tools by registering tool manuals and implementations using addFunctionToUtcpDirectCall, creating a CodeModeUtcpClient, and then calling callToolChain.
import { CodeModeUtcpClient } from '@utcp/code-mode';
import { addFunctionToUtcpDirectCall } from '@utcp/direct-call';
// 1. Register a function that returns a UTCP manual (describes the tool schema)
addFunctionToUtcpDirectCall('getWeatherManual', async () => ({
utcp_version: '0.2.0',
tools: [{
name: 'get_current',
description: 'Get current weather for a city',
inputs: {
type: 'object',
properties: { city: { type: 'string' } },
required: ['city']
},
tool_call_template: {
call_template_type: 'direct-call',
callable_name: 'getWeather'
}
}]
}));
// 2. Register the actual tool implementation
addFunctionToUtcpDirectCall('getWeather', async (city: string) => ({
city,
temperature: 22,
condition: 'sunny'
}));
// 3. Create client and register manual
const client = await CodeModeUtcpClient.create();
await client.registerManual({
name: 'weather',
call_template_type: 'direct-call',
callable_name: 'getWeatherManual'
});
// 4. Execute code with tool access
const { result, logs } = await client.callToolChain(`
const data = weather.get_current({ city: 'London' });
console.log('Weather:', data);
return data;
`);
console.log(result);
// { city: 'London', temperature: 22, condition: 'sunny' }
import { CodeModeUtcpClient } from '@utcp/code-mode';
import { addFunctionToUtcpDirectCall } from '@utcp/direct-call';
// Register a function that returns a UTCP manual
addFunctionToUtcpDirectCall('getWeatherManual', async () => ({
utcp_version: '0.2.0',
tools: [{
name: 'get_current',
description: 'Get current weather for a city',
inputs: {
type: 'object',
properties: { city: { type: 'string' } },
required: ['city']
},
tool_call_template: {
call_template_type: 'direct-call',
callable_name: 'getWeather'
}
}]
}));
// Register the actual tool implementation
addFunctionToUtcpDirectCall('getWeather', async (city: string) => ({
city,
temperature: 22,
condition: 'sunny'
}));
// Create client and register manual
const client = await CodeModeUtcpClient.create();
await client.registerManual({
name: 'weather',
call_template_type: 'direct-call',
callable_name: 'getWeatherManual'
});
// Execute code with tool access
const { result, logs } = await client.callToolChain(`
const data = weather.get_current({ city: 'London' });
console.log('Weather:', data);
return data;
`);
console.log(result);
// { city: 'London', temperature: 22, condition: 'sunny' }