Cookbook
Cookbook
Practical recipes for combining Yolk packages in an app.
Build a stateless /api/agent route
- Accept a request containing
AgentMessage[]. - Decode/validate at the route boundary.
- Resolve enabled tools for the user/session.
- Call
runRuntime({ _tag: 'Transcript', ... }, config). - Provide provider, loop, context transformer, and tool executor layers.
- Encode each
AgentEventas NDJSON or SSE.
Use this shape for simple chat where the client sends the full transcript every turn.
Build a durable session runtime
- Implement
SessionEventStoreagainst your DB, Durable Object storage, or append log. - On new input, call
runRuntime({ _tag: 'AppendInput', ... }, config). - On approval/question response, call
runRuntime({ _tag: 'AppendHitlResponse', ... }, config). - Replay stored events to rebuild transcript and client state.
Use this shape when reconnect, resume, HITL, or server-owned transcript state matters.
Add remote MCP tools to an agent
- Load host-owned MCP server config.
- Call
listMcpToolsorlistRemoteMcpServerTools. - Register each returned
defas a tool withdecodeOutput: mcpToolOutputDecoder(or pass the defs into the loop yourself). - When the model calls one, call
callMcpServerTool; outsideresolveTools, passfileCapture: makeToolFileCapture({ store })so its blobs are stored, not lost. - Return the resulting Yolk
ToolResult(structuredContent{ value, files }) to the loop.
Persist MCP server config and credentials outside the package.
Add knowledge search
- Implement
SearchIndexStore. - Provide extractor, chunker, embedder, and summarizer layers.
- Ingest documents with
ingestKnowledgeDocument. - Search with
searchKnowledge. - Optionally expose
makeKnowledgeLookupToolto the agent.
Resolve search scope from your authenticated app context.
Add external API actions
- Create or choose a connector.
- Build a
ConnectorIntegrationwith config and credential refs. - Provide
CredentialResolverandConnectorHttpClient. - Invoke actions directly or expose them with
makeConnectorToolModule.
Store raw credentials only in host-owned secret storage.
Add command execution
- Choose a sandbox provider layer, such as Vercel Sandbox.
- Provide durable sandbox state storage.
- Scope
sandboxSessionIdto your user/session/run. - Expose
makeSandboxToolModuleonly where command execution is allowed. - Add approval, audit, cleanup, and UI.