Agent / foldkit-agent-a2a
agent-a2a
Letting another AI agent discover and delegate tasks to your Foldkit app over the Agent-to-Agent (A2A) protocol usually requires hand-authoring an Agent Card JSON document and writing a separate JSON-RPC task handler that can drift from your permissions. You want one source of truth for both discovery metadata and runtime execution.
foldkit-agent-a2a generates an A2A Agent Card from your foldkit-agent contract and maps A2A message/send, tasks/get, and tasks/cancel requests directly to your bound AgentRuntime. Availability, authorization, input decoding, cancellation, and completion stay inside your contract. See agent contracts in action in the live Todo agent demo.
Install
pnpm add foldkit-agent foldkit-agent-a2afoldkit, effect, and foldkit-agent are peer dependencies.
A first Agent Card and task handler
Build an AssistantAgent contract (see the Agent contract guide), generate its static Agent Card, and handle an A2A message/send call:
import { Option, Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Agent } from 'foldkit-agent'
import { AgentA2a } from 'foldkit-agent-a2a'
const Model = Schema.Struct({ selectedTodoId: Schema.Option(Schema.String) })
const Message = defineMessageUnion({ RequestedDeleteTodo: { id: Schema.String } })
const TodoAgent = Agent.forModel<typeof Model.Type>()
const AssistantAgent = TodoAgent.make({
messages: TodoAgent.expose(Message, {
RequestedDeleteTodo: {
name: 'delete_todo',
description: 'Delete the selected todo',
available: model => Option.isSome(model.selectedTodoId),
},
}),
})
const card = AgentA2a.agentCard(AssistantAgent, {
name: 'Todos',
description: 'A todo list',
url: 'https://todos.example/a2a',
securitySchemes: { bearer: { type: 'http', scheme: 'bearer' } },
security: [{ bearer: [] }],
})card is plain JSON metadata to serve at AgentA2a.AGENT_CARD_PATH (/.well-known/agent-card.json). Generating the card starts no HTTP server, and your HTTP layer handles authentication and principal binding for incoming requests.
Next, bind the contract to a host and handle incoming A2A JSON-RPC calls:
type AppModel = typeof Model.Type
const { host } = Agent.headless({
init: (): { model: AppModel; commands: [] } => ({
model: { selectedTodoId: Option.some('todo-1') },
commands: [],
}),
update: (model: AppModel) => ({ model, commands: [] }),
})
const agentRuntime = TodoAgent.bind({ definition: AssistantAgent, host })
const served = AgentA2a.handler({ agent: agentRuntime })
await served.handle({
jsonrpc: '2.0',
id: 1,
method: 'message/send',
params: {
message: {
kind: 'message',
role: 'user',
messageId: 'm-1',
parts: [{ kind: 'data', data: { skill: 'delete_todo', input: { id: 'todo-1' } } }],
},
},
})Each A2A skill maps to one exposed capability and passes through the same runtime checks as MCP, WebMCP, and in-process calls. Call served.close() when shutting down; the handler keeps tasks in an in-memory bounded buffer rather than a durable store.
Static card metadata vs. dynamic availability
An Agent Card is static discovery metadata, whereas Foldkit capabilities can depend on live Model state or the caller's principal. The generated card marks dynamic capabilities accurately without locking discovery to a single Model snapshot:
- Capabilities with
available(model)are taggedconditional. - Capabilities with
authorizeare taggedauthorized. - Both checks still run against live state on every
message/send.