Add a tool
Insrc/index.ts, import Worker and the schema builder:
worker.tool:
"lookupCustomer", is the tool key. Use it when you run the tool from the CLI.
Choose a key that is stable and specific. If you rename a tool key, existing agent configuration that refers to the old key needs to be updated.
Describe when the agent should use it
Thetitle and description help Notion Custom Agents and users understand the tool. Keep the title short and write the description as an instruction boundary: what the tool does, and when it should be used.
Define the input schema
Use thej schema builder to define the values your tool accepts. The builder creates a JSON Schema and gives TypeScript types to the execute input. See the Schema and builders reference for all available types.
.describe() on every field. Field descriptions tell the agent what each value means.
Use .nullable() for optional fields:
execute, handle nullable fields explicitly:
additionalProperties: false. Use .nullable() instead of omitting a property from the schema when a value is optional.
Return structured output
A tool can return a string or any JSON-serialisable value. Prefer structured objects for results the agent may need to inspect or reuse:outputSchema. The worker validates the returned value against this schema.
Mark read-only tools
If a tool only reads data and has no side effects, setreadOnlyHint:
Use Notion and external APIs
The second argument toexecute is a context object. Use context.notion to call the Notion API with the worker’s authenticated Notion client. The client has the same permissions as the Custom Agent running the tool:
process.env. For APIs that require user authorization (GitHub, Google, Salesforce), use OAuth instead.
For more on context.notion, see Using Notion API from a worker.
Test a tool locally
Run the tool from your worker project withntn workers exec --local:
.env by default for local execution. To load another file, pass --dotenv:
The Workers runtime injects a preauthenticated Notion SDK client when your
tool runs in Notion, but that client is not available during local execution.
When testing locally, we recommend setting
NOTION_API_TOKEN to a
personal access token in your
.env file, which the SDK uses to create the client..env file, pass --no-dotenv:
Deploy and run the tool
Deploy the worker:ntn workers exec flags and options.
Next steps
Schema and builders
Full reference for the j schema builder and all input types.
SDK reference
Detailed API docs for worker.tool(), hints, and output schemas.
Secrets
Store API keys and credentials for your tools.
Notion API
Read and write Notion data from inside a tool.