Docs/Concepts
Source guide3 min read

Contracts, handlers and adapters

Understand the separate responsibilities that let one operation serve several interfaces.

Download Markdown ↓
Source and verification context ⌄

Source-guided

Examinar el código ↗

Repository links may require access. Source inspection is separate from a clean public-package installation.

AN INTERACTIVE READING MAP

Get the shape of this guide.

Move through the ideas, inspect their relationships, or make a focused reading path. The complete guide stays below.

SECUENCIA DE LECTURAEntender cómo se conectan las ideas
Choose a point to look closer
01 / Sección de la guía

The contract describes the behavior

An operation contract names a command or query and describes its data and requirements. The model includes metadata, input and output schemas, result definitions, policy, declared effects, transport hints and links to related presentations or tests.

Leer el contexto de apoyo

A declaration is useful before it executes: another engineer can inspect its purpose and a tool can discover its shape. But an operation description is not a database implementation, and a policy field is not itself a trusted authorization decision.

Explore a section, then continue with the complete article below.

1 / 4

The contract describes the behavior

An operation contract names a command or query and describes its data and requirements. The model includes metadata, input and output schemas, result definitions, policy, declared effects, transport hints and links to related presentations or tests.

A declaration is useful before it executes: another engineer can inspect its purpose and a tool can discover its shape. But an operation description is not a database implementation, and a policy field is not itself a trusted authorization decision.

The handler performs the work

A handler is the application function bound to a registered operation. It reads or changes the resources your product owns and returns a result that the runtime can validate. It can use host-provided dependencies without putting those providers into the portable declaration.

Business invariants remain your responsibility. A valid input might still refer to an unavailable resource or request a transition the product does not allow. Model those outcomes deliberately and test them in the handler.

The adapter translates an interface

Adapters connect transport-specific requests to the registry. REST handles HTTP input and responses; MCP registers tool shapes and returns tool results. They can call the same operation without having identical wire formats.

The host constructs the context behind those adapters. It establishes identity, binds policy and approval ports, and supplies persistence or provider implementations. Keeping that setup explicit is what lets the contract stay portable.

CapaOwnsDoes not imply
ContractMeaning, shape and declared requirementsAn implemented or authorized effect
Registry and runtimeBinding, common execution and result handlingA production identity system or database
HandlerBusiness implementationAutomatic correctness from its type
AdapterHTTP, MCP or other interface projectionIdentical transport semantics
HostTrusted context and concrete dependenciesAuthority from a browser-supplied role string

Follow a change through the layers

Suppose an export now requires a reason. The contract gains a required input field, callers must provide it, and the handler decides how that reason is used or recorded. Runtime validation can reject an old request shape, while a migration determines whether older consumers remain supported.

This makes a change easier to review: the declaration exposes the intended boundary, and the implementation diff shows the business consequence.

A change has an owner and a set of consequences.Trace the mechanism · 3 ways to look closer

A report export now needs a reason. Follow the change through authored meaning, derived output and a caller that still uses the old input.

Which part are you changing?
UNA SITUACIÓN CONCRETA

The requirement becomes part of declared behavior.

A required reason is added to the operation input.

01 / Author

Change the declaration that owns the required input.

Qué cambia

Callers must now supply the new field for that contract version.

Qué sigue siendo explícito

The handler still decides how the reason affects the business work.

SEGUIR LA CONSECUENCIA

Plan compatibility for existing consumers. A required input can be a behavioral break even if the handler’s main implementation is unchanged.

Where this explanation comes from

An authored situation, grounded in the linked source guides. Choosing a case explores its expected responsibilities; it does not execute an operation.

See it in an example.

Contracts / Beginner

Write your first contract

Define an operation

Follow user.create from an email schema to a typed handler. Start with the smallest maintained example.

  1. Describe the input
  2. Name the operation
  3. Follow the handler

Qué demuestra la fuenteThe maintained source declares user.create with a required email input and a typed output.

No demuestraA database account is created or an email is sent.

Inspect the walkthroughBehavior · proof and limits · source checks

Follow the source behavior

A small user.create command accepts one required email address. Its current handler returns the same email. This gives you a compact place to understand the contract and handler boundary before adding storage or authentication.

  1. Describe the input

    Read UserInput in src/contracts/user.ts. The schema has one required EmailAddress field. The command uses the schema for both input and output.

  2. Name the operation

    Inspect the user.create key, version 1.0.0, owner, goal, and anonymous authentication policy. These describe the operation and its boundary.

  3. Follow the handler

    Open src/handlers/user-input.handler.ts. HandlerForOperationSpec derives the handler shape from CreateUser. The implementation returns input.email; it does not create a persisted account.

  4. Inspect and adapt the starter

    Read the package smoke fixture and validation script. Replace the echo handler with your own behavior, then test that behavior and the contract boundary together.

What this example establishes
Qué demuestra la fuente
  • The maintained source declares user.create with a required email input and a typed output.
  • The handler is typed from the operation specification.
  • A source smoke fixture checks starter metadata and feature registration.
No demuestra
  • A database account is created or an email is sent.
  • This example exposes an MCP tool: its manifest disables MCP.
  • This starter implements an approval gate, persistence, or production authentication.
Reproduce the source checks

The smoke fixture checks metadata and feature wiring; validation checks the contract. Neither is an account-creation integration test.

Directorio de trabajo examples/minimal

bun run smoke
bun run validate

Commands and source inspected; not executed here.

Agents / Intermediate

Draft freely. Send with approval.

Control side effects

Follow one contract boundary through a workflow and an in-memory MCP transport.

  1. Separate the two operations
  2. Inspect the workflow
  3. Inspect the MCP boundary

Qué demuestra la fuenteThe source smoke fixture drives a real MCP server/client over linked in-memory transports.

No demuestraDelivery of a Slack message, email, invitation, or other network request.

Inspect the walkthroughBehavior · proof and limits · source checks

Follow the source behavior

A reference agent drafts a message, waits for approval, and invokes sendMessage. The MCP smoke test uses a real MCP server and client over an in-memory transport. The actual handlers remain deterministic fixture logic.

  1. Separate the two operations

    The source defines draftAndSend.draftMessage and draftAndSend.sendMessage. Drafting is automation-safe; sending declares its approval requirement.

  2. Inspect the workflow

    The example connects draft, approval wait, send, and receipt stages through the WorkflowDevkit adapter. This is a composed reference flow, not an invitation feature in the minimal starter.

  3. Inspect the MCP boundary

    The smoke test registers the operation registry as MCP tools, then calls sendMessage with valid, absent, and tampered receipt evidence. It expects success only for the valid case.

  4. Read what send actually does

    The bound handler derives a message ID and returns status sent in memory. To send through a real provider, supply your own port and preserve the same execution boundary.

What this example establishes
Qué demuestra la fuente
  • The source smoke fixture drives a real MCP server/client over linked in-memory transports.
  • The fixture asserts APPROVAL_REQUIRED for missing and tampered receipts.
  • The same registry gate is used by the MCP adapter rather than a second hand-written approval path.
No demuestra
  • Delivery of a Slack message, email, invitation, or other network request.
  • A fresh successful run or production human approval system.
  • That the minimal example includes MCP; this is a separate source package.
Reproduce the source checks

The source fixtures inspect workflow sequencing and successful versus denied MCP calls with deterministic handlers.

Directorio de trabajo examples/draft-and-send-agent

bun test src/mcp-smoke.test.ts src/draft-and-send.workflow.test.ts

Test and handler source inspected; not executed here.

Continue from here.

Concepts

What the runtime checks

3 min read

Follow a call from transport context to handler execution, then understand the limits of each guard.

Look inside the guideOpening context · complete reading path

Follow the execution boundary

A registered call resolves an operation and its bound handler, prepares the input and execution context, invokes the handler, and validates the returned value. The exact checks depend on the contract and the ports supplied by the host.

Work through the guide
Inicio

Adopt ContractSpec in an existing codebase

3 min read

Wrap one behavior you already own, preserve its observable behavior, and expand from evidence.

Look inside the guideOpening context · complete reading path

Select a small adoption seam

Choose a function or endpoint that already has a clear owner and useful tests. Prefer a contained read or a reversible local action for your first exercise. A workflow that spans several providers, jobs and billing decisions makes it difficult to tell whether a problem comes from your application or the integration.

Work through the guide
Concepts

Model inputs, outcomes and failures

3 min read

Make the data boundary explicit while keeping business meaning in the operation and handler.

Look inside the guideOpening context · complete reading path

Describe what the caller must supply

An input schema should express the shape you actually accept, including required values and constraints that can be checked before execution. Keep the distinction between an absent value and a valid empty value clear.

Work through the guide
Next: What the runtime checks →
Tu vista

Make room for your context.

Shape the presentation around this moment. You can change it whenever you need.

Tu contexto

The different parts of you

Choose a starting point

Una mezcla útil de contexto, imágenes y orientación discreta.

Tune each dimension
Each dimension can change independently. Product permissions and available actions stay unchanged.

Support the way you begin and return

Tune your support

Comodidad de lectura

Elige tu idioma

El idioma de la interfaz solo cambia el texto. Los mercados, precios y flujos de trabajo conservan sus propios ajustes.

Cobertura de traducción

Parte del contenido se muestra en inglés.

Navegación151
Grafo616
Categorías188
Portafolio332
Ejemplos2035
Vista previa de la fuente41

Se añadirán más traducciones a medida que se revise el contenido.

Esta preferencia se guarda en este navegador.

Buscar en el sitio web

Puntos de partida útiles

Sin resultados. Prueba una frase más corta, un nombre de operación u otro ámbito de búsqueda.

UN POCO DE ESPACIO PARA EXPLORAR

Encuentra algo que explorar.

Abre una vista detallada en esta página, prueba otra perspectiva o sigue el detalle a su propia página.

Cómo funcionan las vistas previas y los controles
01

Mirar antes de salir

Pasa el cursor por un enlace o enfócalo con Tab para ver una vista previa. Hacer clic en el enlace original o pulsar Intro siempre abre su destino.

02

Mantener el contexto

Entra en la vista previa para explorar sus secciones. Usa Alt + ↓ desde un enlace enfocado para entrar con el teclado. Escape la cierra.

03

Abrir las posibilidades

Las filas desplegables, las perspectivas y los controles destacados revelan más. Tu vista cambia la comodidad de lectura y el contexto que exploras.

Un botón separado para mirar más de cerca

Muestra pequeños botones de vista previa junto a los enlaces donde haya espacio. Toca el ojo para inspeccionar o el enlace para ir directamente. Esta elección permanece en esta pestaña.

No se recoge historial de navegación. El movimiento reducido sigue tu dispositivo y Tu vista.