Skip to content

Commit aa914a7

Browse files
authored
fix: add methods to fetch builtIn and builtInWrite tools (#581)
1 parent 5923cf8 commit aa914a7

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

runtimes/runtimes/agent.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Agent } from '../server-interface'
1+
import { Agent, ToolClassification } from '../server-interface'
22
import { newAgent } from './agent'
33
import * as assert from 'assert'
44

@@ -178,4 +178,30 @@ describe('Agent Tools', () => {
178178
AGENT.removeTool(SOME_TOOL_SPEC.name)
179179
await assert.rejects(() => AGENT.runTool(SOME_TOOL_SPEC.name, { test: 'test' }), /not found/)
180180
})
181+
182+
it('should track built-in tools', () => {
183+
AGENT.addTool(SOME_TOOL_SPEC, SOME_TOOL_HANDLER, ToolClassification.BuiltIn)
184+
AGENT.addTool({ ...SOME_TOOL_SPEC, name: 'regular' }, SOME_TOOL_HANDLER)
185+
186+
const builtInTools = AGENT.getBuiltInToolNames()
187+
assert.equal(builtInTools.length, 1)
188+
assert.equal(builtInTools[0], SOME_TOOL_SPEC.name)
189+
})
190+
191+
it('should track built-in write tools', () => {
192+
AGENT.addTool(SOME_TOOL_SPEC, SOME_TOOL_HANDLER, ToolClassification.BuiltInCanWrite)
193+
AGENT.addTool({ ...SOME_TOOL_SPEC, name: 'readOnly' }, SOME_TOOL_HANDLER, ToolClassification.BuiltIn)
194+
195+
const builtInTools = AGENT.getBuiltInToolNames()
196+
const builtInWriteTools = AGENT.getBuiltInWriteToolNames()
197+
198+
assert.equal(builtInTools.length, 2)
199+
assert.equal(builtInWriteTools.length, 1)
200+
assert.equal(builtInWriteTools[0], SOME_TOOL_SPEC.name)
201+
})
202+
203+
it('should return empty arrays for built-in tools when none are added', () => {
204+
assert.equal(AGENT.getBuiltInToolNames().length, 0)
205+
assert.equal(AGENT.getBuiltInWriteToolNames().length, 0)
206+
})
181207
})

runtimes/runtimes/agent.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
GetToolsOptions,
77
InferSchema,
88
ObjectSchema,
9+
ToolClassification,
910
Tools,
1011
ToolSpec,
1112
} from '../server-interface'
@@ -21,11 +22,14 @@ type Tool<T, R> = {
2122
export const newAgent = (): Agent => {
2223
const tools: Record<string, Tool<any, any>> = {}
2324
const ajv = new Ajv({ strictSchema: false })
25+
const builtInToolNames: string[] = []
26+
const builtInWriteToolNames: string[] = []
2427

2528
return {
2629
addTool: <T extends InferSchema<S['inputSchema']>, S extends ToolSpec>(
2730
spec: S,
28-
handler: (input: T, token?: CancellationToken) => Promise<any>
31+
handler: (input: T, token?: CancellationToken) => Promise<any>,
32+
toolClassification?: ToolClassification
2933
) => {
3034
const validator = ajv.compile(spec.inputSchema)
3135
const tool = {
@@ -40,6 +44,15 @@ export const newAgent = (): Agent => {
4044
}
4145

4246
tools[spec.name] = tool
47+
if (
48+
toolClassification === ToolClassification.BuiltIn ||
49+
toolClassification === ToolClassification.BuiltInCanWrite
50+
) {
51+
builtInToolNames.push(spec.name)
52+
if (toolClassification === ToolClassification.BuiltInCanWrite) {
53+
builtInWriteToolNames.push(spec.name)
54+
}
55+
}
4356
},
4457

4558
runTool: async (toolName: string, input: any, token?: CancellationToken, updates?: WritableStream) => {
@@ -91,5 +104,13 @@ export const newAgent = (): Agent => {
91104
removeTool: (name: string) => {
92105
delete tools[name]
93106
},
107+
108+
getBuiltInToolNames: () => {
109+
return builtInToolNames
110+
},
111+
112+
getBuiltInWriteToolNames: () => {
113+
return builtInWriteToolNames
114+
},
94115
}
95116
}

runtimes/server-interface/agent.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ export type BedrockTools = {
112112
toolSpecification: Omit<ToolSpec, 'inputSchema'> & { inputSchema: { json: ToolSpec['inputSchema'] } }
113113
}[]
114114

115+
export enum ToolClassification {
116+
BuiltIn = 'builtIn',
117+
BuiltInCanWrite = 'builtInCanWrite',
118+
MCP = 'mcp',
119+
}
120+
115121
export type Agent = {
116122
/**
117123
* Add a tool to the local tool repository. Tools with the same name will be overwritten.
@@ -123,7 +129,8 @@ export type Agent = {
123129
*/
124130
addTool: <T extends InferSchema<S['inputSchema']>, S extends ToolSpec, R>(
125131
spec: S,
126-
handler: (input: T, token?: CancellationToken, updates?: WritableStream) => Promise<R>
132+
handler: (input: T, token?: CancellationToken, updates?: WritableStream) => Promise<R>,
133+
toolClassification?: ToolClassification
127134
) => void
128135

129136
/**
@@ -150,4 +157,16 @@ export type Agent = {
150157
* @param toolName The name of the tool to remove
151158
*/
152159
removeTool: (toolName: string) => void
160+
161+
/**
162+
* Get the list of built-in tool names in the local tool repository.
163+
* @returns The list of built-in tool names
164+
*/
165+
getBuiltInToolNames: () => string[]
166+
167+
/**
168+
* Get the list of built-in write tool names in the local tool repository.
169+
* @returns The list of built-in write tool names
170+
*/
171+
getBuiltInWriteToolNames: () => string[]
153172
}

0 commit comments

Comments
 (0)