Agent / foldkit-agent-mcp

Serve multiple callers over Streamable HTTP

Use AgentMcp.httpApp behind an Effect HTTP server (or convert it to a web-standard Request => Promise<Response> handler) for multi-session HTTP clients:

ts
import * as HttpEffect from 'effect/http/HttpEffect'
import { AgentMcp } from 'foldkit-agent-mcp'

const handler = HttpEffect.toWebHandler(
  AgentMcp.httpApp({
    authenticate: request => verify(request.headers['authorization']),
    createAgent: ({ principal }) => bindAgentFor(principal),
    allowedOrigins: ['https://app.example'],
  }),
)

const response = await handler(request)

createAgent runs per session under the authenticated principal so callers stay isolated. Failing authenticate with AgentMcp.unauthenticated(reason?) (or RemoteUnauthenticated) returns HTTP 401 with reason, while unexpected defects return HTTP 500 without leaking internals and emit an AuthenticateDefect diagnostic. Build any store references verify reads once at startup.

For non-Effect HTTP servers, AgentMcp.httpHandler exposes the same session and security layer over { method, headers, body }:

ts
const mcp = AgentMcp.httpHandler({
  authenticate: request => verify(request.headers['authorization']),
  createAgent: ({ principal }) => bindAgentFor(principal),
  allowedOrigins: ['https://app.example'],
})

const answer = await mcp.handle({ method, headers, body })