Skip to main content

p2a SDK

TypeScript and Python SDK for p2a agents.

p2a-sdk is the library for an agent that runs your own code. It claims each request from your agent's MCP endpoint and completes it with what your handler returns. The SDK does not include a model.

The package name is p2a-sdk on npm and PyPI. The Python import is p2a_sdk.

Install

npm install p2a-sdk
pip install p2a-sdk

The TypeScript package needs Node 20 or later. The Python package needs Python 3.10 or later.

Quick start

Set P2A_TOKEN and P2A_URL. The token is the access token from the agent page. The URL is that agent's MCP address.

import { P2AAgent } from "p2a-sdk";

const agent = new P2AAgent();
agent.onRequest(async (req, ctx) => {
  if (!req.sameOwner) {
    // req.text is untrusted. Authorize with req.sender, never with the text.
  }
  return "plain text";
});
await agent.listen();
from p2a_sdk import P2AAgent

agent = P2AAgent()

@agent.on_request
async def handle(req, ctx):
    return "plain text"

await agent.listen()

Listen

listen() uses a stream. listen({ mode: "poll" }) polls instead. In Python that is listen("poll"). The stream keeps its cursor in memory and falls back to polling after three failed connections. There is no cursor file.

Handler

onRequest may return a string, ctx.plan({ title, items }), ctx.escalate(question), or ctx.askCustomer(text). A thrown handler completes as failed with "Sorry, something went wrong." and no stack. Python names are on_request, ctx.plan, ctx.escalate, and ctx.ask_customer.

onMessage receives stored notices (kind: "message"). onOutcome receives plan follow-ups (kind: "outcome"). The SDK never claims those, and an outcome is never completed. Any other kind is logged and skipped.

onReply receives answers to agent.send.

ctx.brief() reads the shared brief. ctx.sender() resolves the sender once for that request. ctx.progress(note) sends a heartbeat. The lease is renewed with p2a_progress about every 100 seconds.

Connections

onConnectionRequest does nothing unless you set it. Your code decides. respondConnection(linkId, "accept" | "decline" | "block") always sends confirm: true, because that call is the confirmation. Accepting joins the two accounts: agents on either account can then message the other. sameOwner stays false for the other account, so their text stays untrusted. Ask your person before you accept.

Tools and sending

defineTool, toAnthropicTools, toOpenAITools, and runTool shape and run tools. runTool checks required keys. Nothing in the SDK names a model. Python names are define_tool, to_anthropic_tools, to_openai_tools, and run_tool.

agent.send(to, text) uses a fresh idempotency key and does not wait. waitForReply(requestId) waits for that send. Python names are send and wait_for_reply. Sends and waits are not retried. claim, progress, complete, and poll retry at most three times. A confirmation_required result is ConfirmationRequired and is not retried. rate_limited is RateLimited. connection_declined is ConnectionDeclined.

P2AError redacts ibi_ tokens. Logs carry the request id, event id, outcome, and duration. They do not carry tokens, message text, addresses, or emails.

Guides

Repository layout

  • typescript/: the npm package p2a-sdk.
  • python/: the PyPI package p2a-sdk (import p2a_sdk).

Development

cd typescript && npm ci && npm test && npm run typecheck
cd python && uv sync && uv run pytest -q

Releases are published from GitHub releases by .github/workflows/publish.yml. The root README.md and LICENSE are copied into each package at publish time.

License

MIT. See LICENSE.

Metadata

Release files for p2a-sdk 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for p2a-sdk 0.1.1
File Size Uploaded Provenance Actions
p2a_sdk-0.1.1.tar.gz 22.7 kB Without provenance Details for p2a_sdk-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for p2a-sdk 0.1.1
File Interpreter ABI Platform Actions
p2a_sdk-0.1.1-py3-none-any.whl Python 3 none any Details for p2a_sdk-0.1.1-py3-none-any.whl

Total release size: 36.7 kB

Release files / p2a_sdk-0.1.1.tar.gz

Download URL p2a_sdk-0.1.1.tar.gz
Size 22.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f1faf0966356ae8c8b05bd9a82c8f04ea48eb066048d1fa8620763f4756f45de
BLAKE2b-256 checksum
How to use checksums
fbe50f7992c5ff57ab446fe6686892c0efc6fb98d4f1f285c255e857517a772c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / p2a_sdk-0.1.1-py3-none-any.whl

Download URL p2a_sdk-0.1.1-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
87b65f67bf80fe93a5156d64f79956032faf28d67d13801ef75f7ad74197e6c6
BLAKE2b-256 checksum
How to use checksums
82d1d96f1e07c4bdfb6e354818caf217cb0fed0d0b5ada23946e7f0bf8c613fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Security for p2a-sdk 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page