MCPcopy Index your code
hub / github.com/AI45Lab/Code / main

Function main

sdk/node/examples/git/test_register_command.ts:7–45  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5import { Agent } from '../../index.js';
6
7async function main() {
8 const agent = await Agent.create('agent.acl');
9 const session = agent.session('.');
10
11 // Register a custom command
12 session.registerCommand('greet', 'Say hello', (args, ctx) => {
13 const message = `Hello ${args}! Session: ${ctx.sessionId}, Model: ${ctx.model}`;
14 console.log(message);
15 return message; // Note: return value is not captured in current implementation
16 });
17
18 // Register another command
19 session.registerCommand('info', 'Show session info', (args, ctx) => {
20 console.log(`Session Info:`);
21 console.log(` ID: ${ctx.sessionId}`);
22 console.log(` Model: ${ctx.model}`);
23 console.log(` History: ${ctx.historyLen} messages`);
24 console.log(` Tokens: ${ctx.totalTokens}`);
25 console.log(` Cost: $${ctx.totalCost.toFixed(4)}`);
26 console.log(` Tools: ${ctx.toolNames.length}`);
27 return 'Info displayed';
28 });
29
30 // List all commands
31 const commands = session.listCommands();
32 console.log('\nRegistered commands:');
33 commands.forEach(cmd => {
34 console.log(` /${cmd.name} - ${cmd.description}`);
35 });
36
37 // Test the custom commands
38 console.log('\n--- Testing /greet command ---');
39 const result1 = await session.send('/greet World');
40 console.log(`Result: ${result1.text}`);
41
42 console.log('\n--- Testing /info command ---');
43 const result2 = await session.send('/info');
44 console.log(`Result: ${result2.text}`);
45}
46
47main().catch(console.error);

Callers 1

Calls 4

createMethod · 0.45
sessionMethod · 0.45
logMethod · 0.45
sendMethod · 0.45

Tested by

no test coverage detected