Skip to content

feat(sdk): publish the Public Thread client as a minimal Agent App integration kit #489

Description

@Yevanchen

Area

Developer experience / Public Thread API / MCP delegation

Problem

The GoGym dogfood project had to reimplement more than 100 lines of Mosoo-specific integration code before it could embed an Agent safely:

  • Public Thread and Run request/response types
  • Bearer authentication and API error handling
  • SSE parsing and run completion tracking
  • retry/idempotency behavior
  • delegation JWT verification inside the MCP server

This is protocol and trust-boundary glue, not GoGym business logic. Every Agent App would otherwise copy it, and small mistakes create data leaks or duplicate side effects.

Mosoo already contains most of the right low-level implementation in pkgs/public-api-client, verified on main@dd06043:

  • create/retrieve Thread
  • send Message / create Run
  • upload Agent file
  • list and stream run events
  • wait for terminal output
  • typed API errors and idempotency headers

However, the package is currently private: true, and its event stream is a one-shot connection. External apps therefore cannot reuse it and still need to implement reconnect, history reconciliation, deduplication, and delegation verification themselves.

Proposal

Publish and minimally extend the existing @mosoo/public-api-client. Do not create a second SDK or a new application framework.

1. Publish the existing client

  • make the existing package installable and versioned
  • support Node.js and Cloudflare Workers
  • keep the current low-level Thread, Run, File, and event methods

2. Add a resumable watchRun()

A high-level helper should:

  • tail the live event stream
  • reconnect with bounded backoff
  • reconcile persisted event history after reconnect
  • deduplicate by stable event ID
  • check the terminal Thread/Run snapshot
  • support AbortSignal and timeout
  • surface history truncation or terminal failure explicitly
  • never silently replace a missing Thread

The app callback should observe each persisted event at most once within one watcher invocation.

3. Add delegation verification

Provide a runtime-neutral verifyDelegation() helper, implemented with WebCrypto, that validates:

  • signature and allowed algorithm
  • issuer and audience
  • iat, exp, and maximum token lifetime
  • required Mosoo execution claims

Return a typed execution context such as:

{
  userId,
  appId,
  agentId,
  threadId,
  runId,
  toolCallId,
}

This helper verifies Mosoo's signed delegation. It does not implement application login, business authorization, RLS, or replay protection.

4. Make retry-safe idempotency the default

High-level mutation helpers and examples must accept a caller-stable requestId / operation ID and map it to Idempotency-Key.

Documentation must not generate a new random key inside a retry attempt.

Acceptance criteria

  • An external TypeScript app can install the published package.
  • One example backend creates a Thread with trusted userId, watches a Run through a forced disconnect, and never exposes the Mosoo token to the browser.
  • The watcher has tests for reconnect, history merge, duplicate events, terminal state reached during disconnect, timeout, and abort.
  • The delegation helper has tests for valid claims, bad signature, wrong issuer/audience, expiry, future iat, excessive lifetime, and missing claims.
  • Existing low-level client methods remain usable; no parallel client implementation is introduced.
  • GoGym can delete its handwritten Mosoo client/SSE parser and JWT verifier after adopting the package.

Non-goals

  • Supabase Auth, RLS, storage, or application-user lifecycle
  • generating MCP servers, Tool schemas, or business Tools
  • browser WebSocket/UI event mapping
  • Agent manifests or publish/deploy workflows
  • Cloudflare/OpenShip deployment abstraction
  • exactly-once external side effects or provider-specific reconciliation

Related issues

This issue is the thin, reusable adoption layer above those runtime contracts—not a replacement for them.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions