A Python-idiomatic port of the NitroStack Model Context Protocol (MCP) framework, enabling NestJS-like modular architecture, dependency injection, execution pipelines, background task processing, built-in authentication modules, and a diagnostic testing harness.
- Nested Modular Architecture: Group components cleanly with
@module. - Dependency Injection: Explicit class constructor DI with
DIContainerand@injectable(deps=[...]). - Pipeline Interceptors: Build guards, middleware, interceptors, pipes, and exception filters for tool execution.
- Asynchronous Background Tasks: Spawn background workers automatically for long-running tools.
- Built-in Authentication: Modules for API Keys, JWT verification, and OAuth 2.1 (featuring Protected Resource Metadata discovery servers).
- In-Process Testing Harness: Run unit and integration tests against modules without managing subprocesses or real network transports.
- CLI Tooling (
nitrostack-py): Scaffold apps (init), generate components (generate), pack deployable wheels (pack), upgrade/install dependencies, validate projects, auto-register servers with Claude (register), and run hot-reload development servers (dev).
pip install nitrostackTo install local developer or test dependencies:
pip install -e .A security incident affecting eMudhra intermediate certificates means older packaging tools can be man-in-the-middled. Use at least:
- pip 26.2.2
- uv 0.13.4
- certifi 2026.10.19 (this SDK now requires it)
Upgrade pip and uv in existing virtual environments, refresh lockfiles (uv lock --upgrade-package certifi), and rebuild container images. The official python:3.11 and 3.12 Docker images keep an older pip, so upgrade it with the hash-pinned command from the advisory.
nitrostack-py install upgrades the pip in the project .venv to a hash-pinned 26.2.2 before installing anything, and refuses to run uv older than 0.13.4.
You can quickly scaffold a new project template using the interactive CLI tool:
nitrostack-py init(Or via Python: python -m nitrostack.cli.main init)
The project name is optional on the command line. If omitted, the CLI asks for it next:
nitrostack-py init my-server --template python-starterThis launches an interactive prompt where you can:
- Project name (if not passed as an argument). Default:
my-mcp-server. - Choose a template by explicit name:
- python-starter: A simple calculator server.
- python-pizzaz: A pizza shop finder with maps and widgets.
- python-oauth: A flight booking server demonstrating OAuth 2.1 authentication and guarded routes.
- Provide metadata: Specify a custom description and author name.
- Install dependencies:
Install dependencies: (Y/n)— Enter orYrunsnpm installinsrc/widgets;nskips it.--skip-installskips the prompt.
Optional port flags override the defaults (3000 MCP, 3001 widgets):
nitrostack-py init my-server --template python-starter --port 4000 --widget 4001
nitrostack-py dev --port 4000 --widget 4001
nitrostack-py start --port 4000 --widget 4001init and pack vendor this SDK into vendor/nitrostack and pin it with [tool.uv.sources]. The dependency name stays nitrostack in pyproject.toml and requirements.txt, so nitrostack-py install and NitroCloud still run uv sync. Do not put a file: URL in [project].dependencies — that breaks the cloud wheel metadata.
Once scaffolded, follow the next steps printed by the CLI to run your server, configure environment variables, and try it out.
The CLI is installed with the SDK (nitrostack-py, or python -m nitrostack.cli.main). Run nitrostack-py --help to list commands.
Usage telemetry — The CLI sends anonymous usage events to PostHog using a built-in public project key (same CLI/SDK project as the TypeScript CLI). Set NITROSTACK_POSTHOG_KEY to override that key, NITROSTACK_POSTHOG_HOST to override the ingest URL (default https://us.i.posthog.com). Disable with DO_NOT_TRACK=1 or NITROSTACK_TELEMETRY=0. Exit runs a best-effort flush (up to 2s); see docs/analytics-events.md for events, opt-out, and delivery notes.
nitrostack-py init my-server
nitrostack-py dev # hot-reload development server
nitrostack-py start # production server (no reload)
nitrostack-py register --name my-mcp-server --file app.pyExisting tool and module generators are unchanged. Additional generators create pipeline and service stubs that follow the current Python decorator/protocol APIs:
nitrostack-py generate tool add_numbers
nitrostack-py generate module payments
nitrostack-py generate guard MyGuard
nitrostack-py generate pipe Validation
nitrostack-py generate interceptor Transform
nitrostack-py generate filter HttpException
nitrostack-py generate service EmailGenerated files:
| Command | Output |
|---|---|
generate tool <name> |
{name}_tool.py in the current directory |
generate module <name> |
{name}_module.py in the current directory |
generate guard <Name> |
guards/<name>.py |
generate pipe <Name> |
pipes/<name>.py |
generate interceptor <Name> |
interceptors/<name>.py |
generate filter <Name> |
filters/<name>.py |
generate service <Name> |
services/<name>.py |
Attach generated pipeline classes with @use_guards, @use_pipes, @use_interceptors, or @use_filters. Register services in a module's providers list.
nitrostack-py pack --dry-run # list files; does not write an artifact
nitrostack-py pack # write dist/*.whl
nitrostack-py pack --include-env # also pack .env files (use with caution!)pack builds a wheel with setuptools (the same backend as this SDK), refreshes requirements.txt from pyproject.toml when possible, and always includes .env.example. .env, .env.local, and .env.production are excluded unless you pass --include-env. Temporary build directories are deleted afterwards.
nitrostack-py upgrade # move the project to the latest nitrostack on PyPI
nitrostack-py upgrade --version 0.5.0 # move it to exactly this version
nitrostack-py upgrade --dry-run # print the change; do not edit files
nitrostack-py upgrade --allow-downgrade --version 0.1.0 # required to pin an older version
nitrostack-py install # install project + development dependencies
nitrostack-py install --production # skip optional extras and requirements-dev.txt
nitrostack-py validate # lint deps, @mcp_app imports, and @module() refsinit writes pyproject.toml, .python-version, and uv.toml, then runs uv lock when uv is on PATH. install prefers uv sync in that case. If requirements.txt pins nitrostack as a local path (-e /path/to/nitrostack-python-sdk), install uses pip install -r requirements.txt instead so unpublished SDK testing still works. The uv equivalent is:
[tool.uv.sources]
nitrostack = { path = "/path/to/nitrostack-python-sdk", editable = true }then uv lock / uv sync. Without uv, install falls back to .venv + pip.
upgrade in a project with vendor/nitrostack (every project init creates) replaces the vendored copy with the target release: the wheel is downloaded from PyPI and checked against its published sha256, or copied from the running CLI when it is that release. The dependency stays the plain name nitrostack. In other projects, upgrade updates the nitrostack spec in pyproject.toml's dependencies (and requirements.txt when it already pins nitrostack): --version X writes nitrostack==X, and without --version the latest release is written as nitrostack>=latest. Either way, run nitrostack-py install afterwards, and a target older than the current version is rejected unless --allow-downgrade is passed. validate reports missing/conflicting dependencies, @mcp_app modules that fail to import, and @module() imports/exports that are not real classes.
Create a file named app.py:
import asyncio
from pydantic import BaseModel, Field
from nitrostack import (
tool,
resource,
injectable,
module,
mcp_app,
McpApplicationFactory,
ServerConfig,
ExecutionContext,
)
# 1. Input Validation Schema
class AddInput(BaseModel):
a: float = Field(description="First number")
b: float = Field(description="Second number")
# 2. Injected Provider Service
@injectable(deps=[])
class CalculatorService:
def add(self, a: float, b: float) -> float:
return a + b
# 3. Controller
@injectable(deps=[CalculatorService])
class CalculatorController:
def __init__(self, service: CalculatorService):
self.service = service
@tool(
name="add",
description="Add two numbers together",
input_schema=AddInput
)
async def add(self, input: AddInput, context: ExecutionContext) -> float:
context.logger.info(f"Adding {input.a} and {input.b}")
return self.service.add(input.a, input.b)
@resource(
uri="calc://info",
name="Calculator Info",
description="Metadata about this calculator"
)
async def get_info(self, context: ExecutionContext) -> str:
return "Simple Add Calculator v1.0.0"
# 4. Modules
@module(
name="calculator",
controllers=[CalculatorController],
providers=[CalculatorService]
)
class CalculatorModule:
pass
@module(
name="app",
imports=[CalculatorModule]
)
class AppModule:
pass
# 5. Application Entrypoint
@mcp_app(
module=AppModule,
server=ServerConfig(name="math-server", version="1.0.0")
)
class App:
pass
async def main():
app = await McpApplicationFactory.create(App)
await app.start()
if __name__ == "__main__":
asyncio.run(main())The SDK reads settings from the process environment and the project's .env file. The process environment wins: a variable already set when the server starts (for example PORT=3001 from NitroStudio or your shell) is not overwritten by .env. .env fills in what isn't set, and ConfigModule.for_root(defaults=...) fills in the rest. (Before 0.4.1, .env overwrote the environment.)
Read your own settings through ConfigService:
from nitrostack import ConfigModule, ConfigService, injectable, module
@injectable(deps=[ConfigService])
class WeatherService:
def __init__(self, config: ConfigService):
self.units = config.get("WEATHER_UNITS", "metric")
self.api_key = config.get_or_throw("WEATHER_API_KEY") # KeyError if unset
@module(
name="app",
imports=[ConfigModule.for_root(env_file_path=".env", defaults={"WEATHER_UNITS": "metric"})],
providers=[WeatherService],
)
class AppModule:
passCommon transport and telemetry variables:
| Environment Variable | Description |
|---|---|
HOST |
Bind address for HTTP/SSE (default: 127.0.0.1). Set 0.0.0.0 to listen on all interfaces (containers). |
TRUSTED_PROXIES / MCP_TRUSTED_PROXIES |
Comma-separated IPs, CIDRs, or hostnames allowed to send X-Forwarded-Host / X-Forwarded-Proto. Unset means those headers are ignored. X-Forwarded-For is never used to decide trust. |
PORT / MCP_SERVER_PORT |
The port to bind for HTTP/SSE transport (default: 3000). Overridden by nitrostack-py --port. |
WIDGETS_DEV_PORT |
Widget Next.js port (default: 3001). Overridden by nitrostack-py --widget. |
MCP_TRANSPORT_TYPE |
Transport selection: stdio, http, or dual (combining stdio + HTTP/SSE). |
NODE_ENV |
If set to production, defaults to dual transport. Otherwise defaults to stdio. |
MCP_MAX_SESSIONS |
Cap on concurrent Streamable HTTP sessions; new sessions beyond the cap get an HTTP 429. Unset = unlimited. |
MCP_SESSION_TIMEOUT_MS |
Idle timeout (ms) for stateful HTTP sessions; sessions with no activity for this long are terminated automatically. Unset = no timeout. |
MCP_GRACEFUL_SHUTDOWN_TIMEOUT_MS |
How long (ms) the HTTP transport waits for in-flight requests to finish when shutting down (default: 10000). |
NITRO_MCP_PROTOCOL_VERSION |
Protocol era (case-insensitive): auto / both / dual / dual-spec (default when unset or unknown), modern / latest / 2026 / 2026-07-28, or legacy / 2025 / 2025-06-18 / 2025-11-25. auto is not the same as modern. Wins over ServerConfig.protocol_era. |
MCP_STATELESS |
Explicit override: true forces modern (stateless HTTP), false forces legacy (sessionful). Wins over NITRO_MCP_PROTOCOL_VERSION and ServerConfig.protocol_era. |
MCP_ALLOWED_HOSTS / MCP_ALLOWED_ORIGINS |
Comma-separated allow-lists for DNS-rebinding protection, used only when CORS is disabled. |
NITROSTACK_LOG_FILE |
Destination file for logs (default: nitrostack.log). |
NITROSTACK_LOG_LEVEL |
Log level (DEBUG, INFO, WARNING, ERROR). |
NITROSTACK_LOG_TO_STDOUT |
Set to true to allow logging to stdout under stdio transport (Caution: may corrupt protocol stream). |
NITROSTACK_HTTP_DEBUG |
Set to 1 to log every HTTP request (method, path, headers, body) and response status to stderr. Use when diagnosing a client that fails to connect: uvicorn's access log shows neither headers nor timestamps. |
NITROSTACK_POSTHOG_KEY |
Optional. Override the PostHog project key used for CLI telemetry (see docs/analytics-events.md); if unset, a built-in public key is used. |
NITROSTACK_POSTHOG_HOST |
Optional. PostHog ingest host (default https://us.i.posthog.com). |
NITROSTACK_ENVIRONMENT |
production, staging, or development for analytics super-properties. |
NITROSTACK_TELEMETRY |
Set to 0 / false / off to disable CLI telemetry (in addition to DO_NOT_TRACK=1). |
Wrap a secret in SecretValue so it cannot end up in logs, repr, tracebacks, pickles or
serialized output. Everything prints [SecretValue: REDACTED]. The raw value is only available
through unwrap().
from nitrostack import SecretValue, JWTModule, ApiKeyModule
jwt_secret = SecretValue.from_env("JWT_SECRET") # raises if the variable is not set
optional = SecretValue.from_env_optional("MAYBE_SECRET") # None if it is not set
print(f"{jwt_secret}") # [SecretValue: REDACTED]
JWTModule.for_root(secret=jwt_secret)
ApiKeyModule.for_root(keys=[SecretValue.from_env("SERVICE_API_KEY")])SecretValue.from_value("...", allow_hardcoded=True, reason="test")is for values written in code (tests). It must be allowed explicitly and it warns.- Pickling and Pydantic/JSON output are redacted too. A pickled secret loads back with no value, and
unwrap()raises instead of returning a fake one. ==compares withhmac.compare_digest.JWTModule,ApiKeyModule,OAuthModule(the two client secrets) andConfigService(defaults,get_secret()) accept aSecretValueor a plainstr.is_secret_value(x)andunwrap_secret(x)help code that takes either.
One call per auth type returns the keyword arguments for the matching module, and
validate_auth_env lists every missing environment variable at once:
from nitrostack import (
JWTModule, ApiKeyModule, OAuthModule,
setup_jwt_auth, setup_api_key_auth, setup_oauth_auth,
validate_auth_env, print_auth_setup_instructions,
)
ok, missing = validate_auth_env("oauth") # (False, ["RESOURCE_URI", "AUTH_SERVER_URL", ...])
if not ok:
print_auth_setup_instructions("oauth")
JWTModule.for_root(**setup_jwt_auth()) # reads JWT_SECRET
ApiKeyModule.for_root(**setup_api_key_auth()) # reads API_KEY / API_KEY_<n>
OAuthModule.for_root(**setup_oauth_auth()) # reads RESOURCE_URI, AUTH_SERVER_URL, JWKS_URIgenerate_test_credentials() returns random SecretValue credentials for tests and local
demos only. They are never printed; call .unwrap() to read one.
The examples below build on each other (later ones reuse LookupInput and NoInput) and are methods on a controller (an @injectable class listed in a module's controllers). Every tool takes (self, input, context), where input is an instance of its input_schema model and context is the ExecutionContext (logger, auth, task, request metadata).
from pydantic import BaseModel, Field
from nitrostack import tool, initial_tool, ToolAnnotations, ToolExamples, ExecutionContext
class LookupInput(BaseModel):
city: str = Field(description="City name")
class Weather(BaseModel):
city: str
temp: float
units: str
class NoInput(BaseModel):
pass
class WeatherController:
@tool(
name="get_weather",
title="Get Weather",
description="Current weather for a city",
input_schema=LookupInput,
output_schema=Weather, # optional; adds structuredContent
annotations=ToolAnnotations(read_only_hint=True, destructive_hint=False, idempotent_hint=True),
examples=ToolExamples(input={"city": "Paris"}, output={"city": "Paris", "temp": 21, "units": "metric"}),
)
async def get_weather(self, input: LookupInput, context: ExecutionContext) -> dict:
return {"city": input.city, "temp": 21, "units": "metric"}
@tool(name="whats_new", description="Shown when a client connects", input_schema=NoInput)
@initial_tool # clients that support it call this on connect
async def whats_new(self, input: NoInput, context: ExecutionContext) -> str:
return "Try get_weather."A tool with no arguments still needs an input model; use an empty BaseModel subclass like NoInput (not BaseModel itself). visibility="hidden" keeps a tool callable but out of tools/list. Raise an exception to return an MCP error to the client.
from typing import List
from nitrostack import resource, prompt, PromptArgument, PromptMessage, ExecutionContext
class WeatherController:
@resource(uri="weather://units", name="Units", description="Units in use", mime_type="text/plain")
async def units(self, context: ExecutionContext) -> str:
return "metric"
@prompt(
name="forecast_brief",
description="Write a short forecast",
arguments=[PromptArgument(name="city", description="City", required=True)],
)
async def forecast_brief(self, args: dict, context: ExecutionContext) -> List[PromptMessage]:
return [PromptMessage(role="user", content=f"Write a short forecast for {args['city']}.")]Each is a small class with one async method; attach it with a decorator below @tool. Generate stubs with nitrostack-py generate guard|pipe|interceptor|filter <Name>.
| Stage | Method | Attach with | Runs |
|---|---|---|---|
| Guard | can_activate(context) -> bool |
@use_guards(...) |
first; False rejects the call ("Access denied by guard") |
| Pipe | transform(value, metadata) |
@use_pipes(...) |
on the tool input, after guards |
| Middleware | use(context, next_fn) |
@use_middleware(...) |
around interceptors and the tool |
| Interceptor | intercept(context, next_fn) |
@use_interceptors(...) |
around the tool; can change its result |
| Exception filter | catch(error, context) |
@use_filters(...) |
on errors; its return value becomes the result |
from nitrostack import tool, use_guards, use_interceptors, use_filters, ExecutionContext
class OfficeHoursGuard:
async def can_activate(self, context: ExecutionContext) -> bool:
return context.metadata.get("office_open", True)
class TimingInterceptor:
async def intercept(self, context: ExecutionContext, next_fn):
result = await next_fn()
context.logger.info(f"{context.tool_name} finished")
return result
class FriendlyErrors:
async def catch(self, error: Exception, context: ExecutionContext):
return {"error": str(error)}
class WeatherController:
@tool(name="get_forecast", description="Forecast for a city", input_schema=LookupInput)
@use_guards(OfficeHoursGuard)
@use_interceptors(TimingInterceptor)
@use_filters(FriendlyErrors)
async def get_forecast(self, input: LookupInput, context: ExecutionContext) -> dict:
return {"city": input.city, "forecast": "sunny"}Register an auth module in the root module, then guard tools. Guards fill context.auth (subject, scopes, client_id, claims, ...).
| Module | Guard | Configured by |
|---|---|---|
ApiKeyModule.for_root(keys_env_prefix="API_KEY", hashed=False) |
ApiKeyGuard |
x-api-key header checked against API_KEY, API_KEY_1, API_KEY_2, ... (SHA-256 hashes when hashed=True) |
JWTModule.for_root(secret_env_var="JWT_SECRET", audience=..., issuer=...) |
JwtGuard |
Authorization: Bearer <jwt> |
OAuthModule.for_root(resource_uri=..., authorization_servers=[...], scopes_supported=[...], token_introspection_endpoint=... or jwks_uri=...) |
OAuthGuard |
OAuth 2.1 access tokens, audience-bound; OAUTH_REQUIRED=true to require one |
from nitrostack import (
ApiKeyGuard, ApiKeyModule, ExecutionContext, OAuthGuard, module, require_scopes, tool, use_guards,
)
class AccountController:
@tool(name="get_balance", description="Account balance", input_schema=NoInput)
@use_guards(ApiKeyGuard)
async def get_balance(self, input: NoInput, context: ExecutionContext) -> dict:
return {"balance": 42}
@tool(name="close_account", description="Close the account", input_schema=NoInput)
@use_guards(OAuthGuard)
@require_scopes("admin") # needs context.auth with every listed scope
async def close_account(self, input: NoInput, context: ExecutionContext) -> dict:
return {"closed": True}
@module(name="app", imports=[ApiKeyModule.for_root()], controllers=[AccountController])
class AppModule:
passRegister ApiKeyModule when you use ApiKeyGuard: without it, the guard falls back to the API_KEY variable, and with neither set it lets every call through.
has_scope, has_any_scope and has_all_scopes check context.auth inside a tool. For OAuth, start from the python-oauth template and its OAUTH_SETUP.md; PKCE (generate_pkce_params, verify_pkce), client ID metadata documents (resolve_cimd) and generate_www_authenticate_header are exported for custom flows.
from nitrostack import cache, injectable, rate_limit, tool
@injectable()
class WeatherService:
@cache(ttl=60) # seconds; keyed on the arguments
async def lookup(self, city: str) -> dict:
return {"city": city, "temp": 21}
class WeatherController:
@tool(name="ping", description="Ping", input_schema=NoInput)
@rate_limit(max=10, window=60) # 10 calls per 60 s, across all callers
async def ping(self, input: NoInput, context: ExecutionContext) -> str:
return "pong"Put @cache on a service method with plain, hashable arguments (strings, numbers, tuples). It can't key on a tool's Pydantic input object.
Set task_support="optional" (or "required") and the client can run the tool as an MCP task: it polls tasks/get, can cancel it, and gets live notifications/progress when it sends a progressToken. context.task is None when the tool is called normally.
import asyncio
from pydantic import BaseModel
from nitrostack import tool, ExecutionContext
class ReportInput(BaseModel):
steps: int = 5
class ReportController:
@tool(name="build_report", description="Build a report", input_schema=ReportInput, task_support="optional")
async def build_report(self, input: ReportInput, context: ExecutionContext) -> dict:
for step in range(1, input.steps + 1):
if context.task:
context.task.update_progress(f"Step {step}/{input.steps}")
context.task.throw_if_cancelled()
await asyncio.sleep(1)
return {"steps": input.steps}from typing import List
from nitrostack import EventEmitter, health_check, injectable, on_event
@injectable()
class OrderService:
async def place(self, order_id: str) -> None:
await EventEmitter.get_instance().emit("order.placed", {"id": order_id})
@injectable()
class AuditLog:
def __init__(self):
self.entries: List[str] = []
@on_event("order.placed")
def record(self, payload: dict) -> None:
self.entries.append(payload["id"])
@health_check("audit")
def healthy(self) -> bool:
return TrueListeners and health checks are found on any provider or controller in a module. When any health check exists, the server exposes the health://status resource ({"audit": "healthy"}). Keep health checks synchronous.
NitroStack apps can run over three transports, selected via MCP_TRANSPORT_TYPE (or ServerConfig(transport_type=...)):
stdio(default outside production): JSON-RPC over stdin/stdout — the standard mode for desktop MCP clients (Claude Desktop, Cursor, etc.).http: Streamable HTTP + legacy SSE over a real network port, for cloud/remote deployments. Exposes:GET /— documentation landing page (connection setup for Cursor/Claude/ChatGPT plus the registered tool catalog). Same page TypeScript NitroStack serves on Open URL.POST/GET/DELETE /mcp— Streamable HTTP (session-based JSON-RPC + SSE streaming)./mcpand/mcp/are equivalent; the server does not 307 between them (MCP Inspector needs the no-slash URL for its SSE GET).GET /sse+POST /mcp/messages/— legacy HTTP+SSE for older clients (trailing slash required so messages aren't swallowed by the Streamable HTTP/mcpmount)GET /mcp/health— health check (status, active session count, uptime)- Per-session isolation, idle-session timeouts, and DNS-rebinding protection are provided by the underlying
mcpSDK'sStreamableHTTPSessionManager; NitroStack adds CORS, a concurrent-session cap, and the health endpoint on top. - Task-mode tools that call
context.task.update_progress(...)push a livenotifications/progressevent over the session's SSE stream (in addition to always being pollable viatasks/get) whenever the client sends a_meta.progressTokenon thetools/callrequest.
dual(default in production): runsstdioandhttpconcurrently asasynciotasks in the same process/event loop — not separate threads — so both share the sameDIContainersingletons, and uvicorn's signal-based graceful shutdown works correctly (it only installs signal handlers on the main thread). Shutdown is coordinated: either transport stopping (STDIO hitting EOF, or HTTP receiving a termination signal) cleanly stops the other.
Example:
server = ServerConfig(name="my-server", transport_type="http", max_sessions=100, session_timeout_ms=1_800_000)Decorated health checks are exposed at both health://status and health://checks.
The status resource keeps its original {check_name: status} JSON map for existing
clients. The checks resource returns an aggregate payload with status, uptimeSeconds,
per-check name, status, latencyMs, and message, plus count, transforms,
timestamp, and sdkVersion. Checks default to a five-second timeout and run independently;
set a per-check timeout with @health_check("database", timeout=2.0). A false check result
marks the aggregate degraded; a timeout or exception marks it unhealthy (exceptions are
reported with a generic message on health://checks; the legacy status resource retains its
existing error string). A timed-out check has status timeout.
Async checks are timed out on the event loop. Sync checks run in a dedicated four-worker pool,
with at most four submitted sync checks at a time; a timed-out sync function keeps its worker
slot until it returns, but does not hold up the resource response or asyncio.run() shutdown.
Executor threads are non-daemon, so a sync check that never returns can still delay process exit.
Sync checks therefore run off the main thread and should not depend on thread-bound resources
such as a database connection created on the main thread. Transform entries currently report
their name, type, and order; details is reserved for telemetry that is not yet collected.
The SDK version is resolved once when the package loads.
ServerHealthPayload and TransformTelemetry are exported from nitrostack for typing.
ServerConfig.protocol_era is used only when MCP_STATELESS and NITRO_MCP_PROTOCOL_VERSION are both unset. Unknown tokens become auto, same as an unknown env value.
Tools can declare visibility="hidden" and default_visible=False, then handlers can
call context.enable_tools(["admin_tool"]) or context.disable_tools([...]). Overrides
are scoped by transport session ID and verified token subject, with a bounded TTL/LRU
store. Dynamic visibility requires a sessionful transport. The default auto era is
sessionless, so changes warn and do nothing there; the SDK does not fall back to
subject-only state because it would share overrides across that subject's sessions.
To automatically configure your server script with Claude Desktop without any manual editing:
nitrostack-py register --name my-mcp-server --file app.py(If your scripts folder is not in PATH, use: python -m nitrostack.cli.main register --name my-mcp-server --file app.py)
This detects all standard and Windows Store installation directories, sets up virtualenv executables, and writes the JSON configuration. Once registered, simply restart Claude Desktop.
Applications with registered widgets expose an application/json manifest at
widget://examples. Discover it through MCP resources/list and read it with
resources/read, using {"uri": "widget://examples"} as the request parameters.
The manifest contains widgets, count, and loaded; each widget includes its
route, URI, mode-specific MIME type, metadata, associated tool names, and any
declared ToolExamples inputs and outputs. It is built at startup from registered
decorators, including helper-generated options, without executing tools.
For Python access:
from nitrostack import get_widget_registry
registry = get_widget_registry(app) # also available as app.widget_registry
widgets = registry.get_all_widgets()
widget_info = registry.get_widget("ui://widget/card.html") # None if absent
manifest = registry.get_manifest().to_dict()Each application owns its registry, and reads return detached copies. Shared
routes appear once in sorted order, retaining examples from every associated
tool. Applications without widgets expose no additional resource. In widget
applications, widget://examples is reserved; a conflicting resource registration
raises a startup error. Existing widget HTML resources and rendering are unchanged.
Bind a static HTML template to a tool with @widget and return domain JSON from the handler:
from nitrostack import tool, widget, ExecutionContext
@tool(name="show_card", description="Product card", input_schema=CardInput)
@widget("card")
async def show_card(self, input: CardInput, context: ExecutionContext) -> dict:
return {"name": "Widget", "price": 9.99}Place HTML at widgets/out/{route}.html (e.g. widgets/out/card.html). The SDK registers
ui://widget/card.html as an MCP resource and sets mode-gated _meta on tools/list and
tools/call results.
NITROSTACK_APP_MODE (default openai; also accepts APP_MODE):
| Mode | Tool _meta |
Resource MIME |
|---|---|---|
universal (default) |
Both OpenAI and MCP Apps keys | text/html;profile=mcp-app |
openai |
openai/outputTemplate, ui/template |
text/html |
mcp-app |
_meta.ui (resourceUri, visibility, CSP) |
text/html;profile=mcp-app |
Object form for CSP and border options:
from nitrostack import WidgetOptions, WidgetCsp, widget
@widget(WidgetOptions(
route="chart",
prefers_border=True,
csp=WidgetCsp(connect_domains=["https://api.example.com"]),
))
async def show_chart(self, input, context):
...nitrostack-py init copies widgets/out/{route}.html for every @widget. Widget HTML
is generated in Python from the tool's structuredContent (one iframe, N cards).
MCP Inspector: use HTTP + stateless, then the Apps tab. tools/call also embeds
the data-filled HTML. Do not use widgets/preview.html as the live result — that file
is a static helper. Live preview: http://localhost:3000/widgets/preview.
Turn Authentication off in Inspector. Pizzaz/starter have no OAuth. If Auth is on,
Inspector POSTs /register and you will see Cannot POST /register / Unexpected token '<'.
Connect Streamable HTTP to http://localhost:3000/mcp (no trailing slash).
For open pizza shops only, call show_pizza_list with {"openNow": true} or
show_pizza_map with {"filter": "open_now"}. Omitting those fields returns every shop,
including closed ones (Pizzeria Delfina).
NitroStudio: folder-connect looks for a TypeScript project (package.json with
@nitrostack/core and src/index.ts). A Python server will not detect. Keep using
MCP Inspector over HTTP, or point Studio at a custom Streamable HTTP URL if the build
supports it. Do not enable OAuth against this server.
Example server: examples/widgets_example.py with templates in examples/widgets/out/.
For MCP Inspector over HTTP, use stateless mode:
cd examples
MCP_TRANSPORT_TYPE=http MCP_STATELESS=true NITROSTACK_APP_MODE=universal python widgets_example.pyUse OAuth2Client for authorization-code/PKCE and client-credentials flows,
managed token storage, refresh, discovery, registration and revocation. Tokens
stay in memory unless a file store is explicitly configured. See the
OAuth client guide for examples and security responsibilities.
To run the automated test suite, execute:
python tests/test_basic.py
python tests/test_tasks.py
python tests/test_initial_tool.py
python tests/test_transports.py
python tests/test_widgets.py
python tests/test_widget_metadata.py
python tests/test_pizzaz_widgets.py
python tests/test_template_widgets.py
python tests/test_cli.py
pytest tests/test_cli.py -v
python tests/test_tool_input_schema.pyWrite in-process unit tests using the harness:
import asyncio
from nitrostack.testing import NitroTestingModule
from app import AppModule
async def test_add():
harness = await NitroTestingModule.create(AppModule)
result = await harness.call_tool("add", {"input": {"a": 5, "b": 10}})
assert result == 15.0
print("Test passed!")
if __name__ == "__main__":
asyncio.run(test_add())Replace a provider with a fake, and release resources when the test ends:
import asyncio
from pydantic import BaseModel
from nitrostack import ExecutionContext, module, injectable, tool
from nitrostack.testing import NitroTestingModule, MockLogger, create_mock_context
class ItemInput(BaseModel):
key: str
@injectable()
class Database:
def __init__(self):
raise RuntimeError("the real database must not be built in tests")
class FakeDatabase:
closed = 0
def get(self, key):
return f"fake-{key}"
def on_module_destroy(self): # provider shutdown hook; runs once on close()
FakeDatabase.closed += 1
@injectable(deps=[Database])
class Items:
def __init__(self, db):
self.db = db
@tool(name="get_item", description="Look up an item", input_schema=ItemInput)
async def get_item(self, input: ItemInput, context: ExecutionContext) -> str:
return self.db.get(input.key)
@module(name="items", providers=[Database, Items])
class AppModule:
pass
async def test_get_item():
async with await NitroTestingModule.create(AppModule, overrides={Database: FakeDatabase()}) as app:
assert await app.call_tool("get_item", {"input": {"key": "a"}}) == "fake-a"
assert FakeDatabase.closed == 1 # close() ran the hook; calling it again would not repeat it
logger = MockLogger()
context = create_mock_context(logger=logger)
context.logger.info("hello")
assert logger.has_log("info", "hello")
if __name__ == "__main__":
asyncio.run(test_get_item())overrides takes an instance, or a class/function that builds one (it may be async). Overrides
are undone by close(). Shutdown hooks are methods named on_module_destroy (sync or async).
flush_tasks() yields to the event loop so background tasks can finish. The optional pytest
fixture nitro_client is enabled with pytest_plugins = ["nitrostack.testing.pytest_plugin"].
Attach DataSpilloverInterceptor to a tool to store large results separately and
return a preview, resource_uri, and original_size_bytes. Results at or below
the threshold pass through unchanged. Sizes count serialized UTF-8 bytes, not
characters; lists and dictionaries use compact JSON.
from pydantic import BaseModel
from nitrostack import DataSpilloverInterceptor, MemorySpilloverStore, tool, use_interceptors
spillover = DataSpilloverInterceptor(
threshold_bytes=64 * 1024,
store=MemorySpilloverStore(
max_bytes=10 * 1024 * 1024,
max_total_bytes=100 * 1024 * 1024,
ttl_seconds=300,
),
resource_uri_prefix="resource://data-spillover/",
)
class ReportInput(BaseModel):
count: int = 10000
class ReportTools:
@tool(name="report", description="Generate a report", input_schema=ReportInput)
@use_interceptors(spillover)
async def report(self, input: ReportInput, context):
return [{"row": index} for index in range(input.count)]Register ReportTools as a module provider or controller as usual. A configured
interceptor instance works directly in the decorator. For default configuration,
@use_interceptors(DataSpilloverInterceptor) also works through dependency injection.
The MCP result includes a text preview, structured content, and a resource link.
Clients retrieve the full result with resources/read, for example:
{
"jsonrpc": "2.0",
"id": 2,
"method": "resources/read",
"params": {"uri": "resource://data-spillover/<id-from-tool-result>"}
}Lists and dictionaries preview their first five items or entries and total counts. Strings preview up to 512 characters. The complete preview stays within 2 KiB; large nested values are represented as truncated text. Reading the full resource can consume context again, so clients should retrieve it only when needed.
For disk storage, replace the store with:
from nitrostack import FileSpilloverStore
store = FileSpilloverStore(
".nitrostack/spillover",
max_bytes=10 * 1024 * 1024,
max_total_bytes=100 * 1024 * 1024,
ttl_seconds=300,
)The memory store evicts least recently used results when its payload capacity is
full. The file store rejects writes when its capacity is full; its capacity counts
actual JSON file bytes, including base64 and metadata overhead. A single result
larger than max_bytes raises PayloadTooLargeError. Both stores remove expired
results during operations; file storage also cleans up on initialization. Expiry
is absolute and reads do not extend it. Each file-store root must be owned by one
store instance in one process; shared storage across processes is not supported.
Keep the configured directory private to the server account.
Resource access requires the same verified principal, issuer, client identity,
and tenant. Anonymous access is limited to the same server-side MCP session.
Requests without either a verified identity or trusted session cannot spill
results; invalid credentials do not fall back to anonymous access. Anonymous
HTTP clients need a persistent MCP session; sessionless HTTP clients should
authenticate. Expired results and results owned by another caller return
resource-not-found. Stored results are not enumerated in resources/list.
HTTP ownership uses the server-issued session ID after the official transport has validated it, rather than the identity of an SDK request/session proxy. Stdio assigns a random identity to each live stream connection. Requests on that connection can retrieve its anonymous results; a later connection cannot inherit them. Concurrent HTTP requests cannot borrow a stdio connection's identity.
Successful explicit MCP tool results are stored as JSON; errors and input-required responses pass through. Decorated tools with output schemas advertise either the original output or the spillover envelope. Spilled widget results return the resource reference without invoking the widget's data transformer.
Independent HTTP/stdio tests and reproducible large-output checks live in
tests/test_spillover_transport.py. Run the spillover tests with
python -m pytest tests/test_spillover.py tests/test_spillover_transport.py.
| Template | Example |
|---|---|
python-enterprise-search |
BM25 discovery over local finance, inventory and support tools |
python-code-mode |
Bounded JavaScript workflows calling Python tools through native QuickJS |
nitrostack-py init search-demo --template python-enterprise-search --skip-install
nitrostack-py init code-demo --template python-code-mode --skip-installEach generated README explains installation, tool calls, validation, upgrade and
packaging. Both use deterministic sample data without credentials or external
services. Code-mode execution additionally requires quickjs>=1.19.4; its workers
start lazily and are disposed on graceful shutdown. Existing starter, pizzaz and
OAuth templates remain available.