@barry-rocks/sdk/tools
Helpers for writing tools: the tool contract (defineTool, the same
function as Barry.Tool, and its ToolContext), definePrompt for prompt
contributors, resolveSessionId, resolveUserPath, result capping, and
mcpResult for returning native MCP content. It also holds the adapters that
present tools on a command line and over MCP.
import { … } from "@barry-rocks/sdk/tools";Interfaces
Section titled “Interfaces”AdaptedMcpTools
Section titled “AdaptedMcpTools”Properties
Section titled “Properties”| Property | Type |
|---|---|
currentContext |
() => ToolContext | undefined |
registerMcpResources |
(server, context) => void |
tools |
AnyToolDefinition[] |
AdaptMcpToolsOptions
Section titled “AdaptMcpToolsOptions”Properties
Section titled “Properties”| Property | Type |
|---|---|
access |
Record<string, "read" | "write"> |
namespace |
string |
register |
(server) => void |
secrets |
string[] |
CapOptions
Section titled “CapOptions”Properties
Section titled “Properties”CappedText
Section titled “CappedText”Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
omitted |
number |
Characters removed (0 when not truncated). |
text |
string |
- |
truncated |
boolean |
- |
CliOption
Section titled “CliOption”Properties
Section titled “Properties”| Property | Type |
|---|---|
choices? |
string[] |
coerce? |
(value) => unknown |
defaultValue? |
unknown |
description |
string |
flags |
string |
required |
boolean |
CliPositional
Section titled “CliPositional”Properties
Section titled “Properties”| Property | Type |
|---|---|
description |
string |
key |
string |
syntax |
string |
CliToolSpec
Section titled “CliToolSpec”Properties
Section titled “Properties”| Property | Type |
|---|---|
command |
string |
description |
string |
excluded |
boolean |
keyMap |
Map<string, string> |
options |
CliOption[] |
positional |
CliPositional | null |
McpAnnotations
Section titled “McpAnnotations”Properties
Section titled “Properties”| Property | Type |
|---|---|
destructiveHint? |
boolean |
idempotentHint? |
boolean |
openWorldHint? |
boolean |
readOnlyHint? |
boolean |
title? |
string |
McpServerLike
Section titled “McpServerLike”Methods
Section titled “Methods”registerTool()
Section titled “registerTool()”registerTool(
name,config,handler):unknown
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
name |
string |
config |
McpToolConfig |
handler |
(args, extra) => Promise<unknown> |
Returns
Section titled “Returns”unknown
resource()
Section titled “resource()”resource(…
args):unknown
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
…args |
unknown[] |
Returns
Section titled “Returns”unknown
McpToolConfig
Section titled “McpToolConfig”Properties
Section titled “Properties”| Property | Type |
|---|---|
_meta? |
Record<string, unknown> |
annotations? |
Record<string, unknown> |
description? |
string |
inputSchema? |
Record<string, unknown> |
PromptContext
Section titled “PromptContext”Context supplied to a bag’s computed prompt entry.
Properties
Section titled “Properties”| Property | Type |
|---|---|
cwd |
string |
provider? |
string |
repo? |
string |
sessionId? |
string |
PromptSection
Section titled “PromptSection”One complete section contributed to the system prompt.
Properties
Section titled “Properties”| Property | Type |
|---|---|
text |
string |
RegisterToolsOptions
Section titled “RegisterToolsOptions”Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
onToolCall? |
(event) => void |
- |
resultCharLimit? |
number |
Off by default; the consuming host chooses its output budget. |
ToolContext
Section titled “ToolContext”Properties
Section titled “Properties”ToolDefinition
Section titled “ToolDefinition”Type Parameters
Section titled “Type Parameters”| Type Parameter | Default type |
|---|---|
T extends ToolShape |
ToolShape |
R |
unknown |
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
_meta? |
Record<string, unknown> |
- |
access |
"read" | "write" |
- |
annotations? |
McpAnnotations |
- |
cliFormat? |
(result) => string |
- |
deferred? |
boolean |
Barry’s host can hide callable tools until tool_search discovers them. |
description |
string |
- |
handler |
(params, context?) => Promise<R> |
- |
name |
string |
- |
namespace |
string |
- |
outputSchema? |
Record<string, unknown> |
JSON Schema avoids tying result contracts to a particular Zod version. |
schema |
T |
- |
secrets? |
string[] |
- |
Type Aliases
Section titled “Type Aliases”AnyToolDefinition
Section titled “AnyToolDefinition”AnyToolDefinition =
ToolDefinition<any,any>
ListToolsHandler
Section titled “ListToolsHandler”ListToolsHandler = (
request,extra) =>Promise<{tools?:Record<string,unknown>[]; }>
Normalize the JSON Schema this server puts on the wire.
The SDK converts each tool’s zod schema when it answers tools/list, picking its converter from the major that BUILT the schema – so without this, a bag’s published contract changes when its author bumps zod. Wrapping the handler is the only place to catch it: registerTool takes a zod schema, not JSON Schema, so there is nothing to normalize on the way in.
The SDK keeps its handlers in a private _requestHandlers map with no
public getter. Reading it is load-bearing: if a REAL server has the map but no
tools/list handler in it, the SDK’s internals moved and we must NOT silently
skip – that would ship an un-normalized schema while every test stayed
green – so that case throws. A server with no map at all is a test double
with no wire to protect, and is skipped.
Idempotent and version-agnostic, so it is safe on a server whose tools came from several realms at once – the normal case for a bag host.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
request |
unknown |
extra |
unknown |
Returns
Section titled “Returns”Promise<{ tools?: Record<string, unknown>[]; }>
OptionalKeys
Section titled “OptionalKeys”OptionalKeys<
T> ={ [K in keyof T]: undefined extends OutputOf<T[K]> ? K : never }[keyofT]
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends ToolShape |
OutputOf
Section titled “OutputOf”OutputOf<
S> =Sextendsobject?O:Sextendsobject?O:never
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
S |
PromptContributor
Section titled “PromptContributor”PromptContributor = (
context) =>PromptSection[] |Promise<PromptSection[]>
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
context |
PromptContext |
Returns
Section titled “Returns”PromptSection[] | Promise<PromptSection[]>
ToolParams
Section titled “ToolParams”ToolParams<
T> ={ [K in Exclude<keyof T, OptionalKeys<T>>]: OutputOf<T[K]> }&{ [K in OptionalKeys<T>]?: OutputOf<T[K]> }
What z.infer<z.ZodObject<T>> gives, computed from _output so it works for both majors.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends ToolShape |
ToolParamSchema
Section titled “ToolParamSchema”ToolParamSchema = {
_output:unknown; } | {_zod: {output:unknown; }; }
Accept parameter schemas from either supported zod major through their common output member, without requiring one major’s private schema internals. See docs/decisions/0011-sdk-runtime-invariants.md for the retained rationale.
ToolShape
Section titled “ToolShape”ToolShape =
object
Index Signature
Section titled “Index Signature”[key: string]: ToolParamSchema
Variables
Section titled “Variables”DEFAULT_RESULT_CHAR_LIMIT
Section titled “DEFAULT_RESULT_CHAR_LIMIT”
constDEFAULT_RESULT_CHAR_LIMIT:100000=100_000
Functions
Section titled “Functions”adaptMcpTools()
Section titled “adaptMcpTools()”adaptMcpTools(
options):AdaptedMcpTools
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options |
AdaptMcpToolsOptions |
Returns
Section titled “Returns”buildCliSpec()
Section titled “buildCliSpec()”buildCliSpec<
T,R>(tool):CliToolSpec
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends ToolShape |
R |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
tool |
ToolDefinition<T, R> |
Returns
Section titled “Returns”buildToolCallback()
Section titled “buildToolCallback()”buildToolCallback(
tool,options?): (params) =>Promise<{[key:string]:unknown; }>
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
tool |
AnyToolDefinition |
options |
RegisterToolsOptions |
Returns
Section titled “Returns”(params) => Promise<{[key: string]: unknown; }>
capText()
Section titled “capText()”capText(
text,opts?):CappedText
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
text |
string |
opts |
CapOptions |
Returns
Section titled “Returns”capToolResult()
Section titled “capToolResult()”capToolResult<
T>(result,limit?):T
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
result |
T |
limit? |
number |
Returns
Section titled “Returns”T
definePrompt()
Section titled “definePrompt()”definePrompt<
T>(contributor):T
Mark a function as a bag prompt entry and retain its inferred type.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends PromptContributor |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
contributor |
T |
Returns
Section titled “Returns”T
defineTool()
Section titled “defineTool()”defineTool<
T,R>(definition):ToolDefinition<T,R>
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends ToolShape |
R |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
definition |
ToolDefinition<T, R> |
Returns
Section titled “Returns”ToolDefinition<T, R>
formatOutput()
Section titled “formatOutput()”formatOutput<
R>(result,tool,json):string
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
R |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
result |
R |
tool |
ToolDefinition<ToolShape, R> |
json |
boolean |
Returns
Section titled “Returns”string
getHandlerMap()
Section titled “getHandlerMap()”getHandlerMap(
server):Map<string,ListToolsHandler> |undefined
The SDK’s live protocol-handler map. The sole sanctioned reach-in here; Undefined for a test double with no inner server.
Exported for register-normalize-guard.test.ts, which has to inspect the same map to prove the wrap took effect. Sharing this keeps the assertion in one place rather than repeating the cast at every call site.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
server |
McpServer |
Returns
Section titled “Returns”Map<string, ListToolsHandler> | undefined
isMcpResult()
Section titled “isMcpResult()”isMcpResult(
value):value is { [key: string]: unknown }
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
value |
unknown |
Returns
Section titled “Returns”value is { [key: string]: unknown }
mcpResult()
Section titled “mcpResult()”mcpResult<
T>(response):T
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T extends object |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
response |
T |
Returns
Section titled “Returns”T
registerTools()
Section titled “registerTools()”registerTools(
server,tools,options?):void
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
server |
McpServer |
tools |
AnyToolDefinition[] |
options |
RegisterToolsOptions |
Returns
Section titled “Returns”void
resolveParams()
Section titled “resolveParams()”resolveParams(
spec,positionalValue,options):Record<string,unknown>
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
spec |
CliToolSpec |
positionalValue |
string | undefined |
options |
Record<string, unknown> |
Returns
Section titled “Returns”Record<string, unknown>
resolveSessionId()
Section titled “resolveSessionId()”resolveSessionId(
context?):string
Resolve the session id preferring the per-request context (set by the shared HTTP MCP server) and falling back to the env var (CLI stdio mode). The shared server has no per-session BARRY_SESSION_ID, so context.sessionId is the only reliable source there.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
context? |
ToolContext |
Returns
Section titled “Returns”string
resolveUserPath()
Section titled “resolveUserPath()”resolveUserPath(
filePath):string
Resolve a user-supplied path the way the builtin tools do: ~/~/...
expands against HOME, anything else resolves against the process working
directory. Shared so path-based policy (scope-guard deny patterns,
read-tracking keys) sees the same normalized path the tools actually touch.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
filePath |
string |
Returns
Section titled “Returns”string
resultCharLimitFromEnv()
Section titled “resultCharLimitFromEnv()”resultCharLimitFromEnv():
number
Returns
Section titled “Returns”number
toolNameToCommand()
Section titled “toolNameToCommand()”toolNameToCommand(
toolName,namespace):string
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
toolName |
string |
namespace |
string |
Returns
Section titled “Returns”string
withCredentialGuidance()
Section titled “withCredentialGuidance()”withCredentialGuidance(
message):string
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
message |
string |
Returns
Section titled “Returns”string