Skip to main content

unsworn (Python SDK)

Write Unsworn handlers in Python. A handler is a plain function, sync or async:

from unsworn import json

def get_user(req, ctx):
    # req.params["id"] is already validated and converted by Unsworn,
    # and ctx["auth"] holds the verified token claims.
    return json({"id": req.params["id"], "name": "Ada"})
# unsworn.toml
[runtimes.python]
extensions = [".py"]
command = "python3 -u -m unsworn.worker"
protocol = "framed"

[[route]]
path = "/users/{id}"
method = "GET"
handler = "src/users.py#get_user"
params = { id = { type = "integer", min = 1 } }

Replies

Return Response
json(value, status=200, headers=None) JSON body
text(value, status=200, headers=None) Text body
body(bytes_or_iterator, status=200, headers=None) Raw bytes, or streamed when given an iterator or async iterator
{"status": 204} No body
next({"tenant": "acme"}) From a before handler: continue, adding context

MCP tools

A handler listed in [[mcp.tool]] is an ordinary handler. Its arguments arrive as req.json, already validated, and ctx["mcp"] has the tool name, protocol_version and, when the client sends it, client. Unsworn turns the reply into an MCP tool result (see docs/mcp.md):

from unsworn import content, json, tool_error

def get_forecast(req, ctx):
    city = req.json["city"]
    if city not in CITIES:
        return tool_error(f"unknown city {city}")  # the model sees this
    return json({"city": city, "days": forecast(city)})

def chart(req, ctx):
    return content([{"type": "image", "data": png_b64, "mimeType": "image/png"}])
Return Tool result
json(value) Structured content, plus a text copy
text(value) One text block
content(blocks, json=value) MCP content blocks as given; with json, structured content too
tool_error(message) isError: true with message

The request

req has method, path, route, params, query, raw_query, headers (lowercase names), json, text, body (raw bytes), handler, phase, and cancelled (set when Unsworn cancels the request). ctx is a dict with auth (verified claims) and anything before handlers added.

On routes with stream_body = true, req.stream yields the body in chunks as it arrives: async for chunk in req.stream, or for in a sync handler.

Sync handlers run in a thread pool, so they don't block other requests. print() output goes to stderr, which Unsworn collects into its logs.

Logging

The worker sends records from Python's logging module to Unsworn, tagged with the request being handled. extra values are kept as fields:

import logging

log = logging.getLogger(__name__)

def charge(req, ctx):
    log.info("charged card", extra={"amount": 1200})

The worker sets the root logger's level from Unsworn's, so there's nothing to configure.

Metadata

Release files for unsworn 0.3.0

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

Source distribution (sdist)

Source distribution for unsworn 0.3.0
File Size Uploaded Provenance Actions
unsworn-0.3.0.tar.gz 8.9 kB Without provenance Details for unsworn-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for unsworn 0.3.0
File Interpreter ABI Platform Actions
unsworn-0.3.0-py3-none-any.whl Python 3 none any Details for unsworn-0.3.0-py3-none-any.whl

Total release size: 19.0 kB

Release files / unsworn-0.3.0.tar.gz

Download URL unsworn-0.3.0.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9adfd2f9ae89791d3488269a5b6f396c828b501c2850aa302687d1de5ebf2877
BLAKE2b-256 checksum
How to use checksums
159e9214d1bb3a4138077c1bc631c166314f040b75002360327b29a729c9c4c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Omarchy","version":"4.5.0","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / unsworn-0.3.0-py3-none-any.whl

Download URL unsworn-0.3.0-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f14ce9c2d01e8fdfc44ff2ab32e6580c514ed3d087eed7416df8e967550cfd1c
BLAKE2b-256 checksum
How to use checksums
a9ae2c6ac9c71e8d0ac7a19c88d1484740d1df49eb5b02b3650b632bb857a2c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Omarchy","version":"4.5.0","id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for unsworn 0.3.0

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.

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