Applications

Mountable ASGI application types.

AsgiApplication

Base class for all genro-asgi applications.

AsgiApplication package.

class genro_asgi.applications.asgi_application.AsgiApplication(**kwargs)[source]

Bases: RoutingClass

Base class for apps mounted on AsgiServer.

Provides default main router and index() method. Subclasses define openapi_info for metadata and add routes with @route() decorator.

Example:

class MyApp(AsgiApplication):
    openapi_info = {"title": "My API", "version": "1.0.0"}

    @route()  # Uses the only router (self.main) automatically
    def hello(self):
        return "Hello!"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)  # Required: creates self.main
        self.backoffice = Router(self, name="backoffice")

    @route("backoffice")  # Must specify when multiple routers
    def admin(self):
        return "Admin panel"
__init__(**kwargs)[source]

Initialize app with default main router.

Parameters:

**kwargs (Any) – Passed to on_init() after extracting base_dir and db_name.

app_protocol: ClassVar[str] = 'asgi'
default_db_name: ClassVar[str | None] = None
index()[source]

Return HTML splash page. Override for custom index.

Return type:

str

load_resource(*args, name)[source]

Load resource file content with mime type.

When mounted on server: uses server’s ResourceLoader with fallback to local. Standalone: reads directly from resources_dir.

Return type:

tuple[bytes, str] | None

Returns:

Tuple of (content_bytes, mime_type) or None if not found.

property mount_name: str

Return the name under which this app is mounted on the server.

on_init(**kwargs)[source]

Called after base initialization. Override for custom setup.

Parameters:

**kwargs (Any) – Parameters from config.yaml app definition.

Return type:

None

on_shutdown()[source]

Called when server stops. Override for custom cleanup.

Can be sync or async. Called in reverse order of startup.

Return type:

None

on_startup()[source]

Called when server starts. Override for custom initialization.

Can be sync or async. Called after all apps are mounted.

Return type:

None

openapi_info: ClassVar[dict[str, Any]] = {}
property resources_dir: Path | None

Return path to app’s resources directory.

Works both when mounted on server and standalone.

property server: AsgiServer | None

Return the server that mounted this app (semantic alias for _routing_parent).

McpApplication

MCP Streamable HTTP transport application.

McpApplication package.

class genro_asgi.applications.mcp_application.McpApplication(**kwargs)[source]

Bases: AsgiApplication

MCP Streamable HTTP transport as a genro-asgi application.

Accepts an external genro-routes Router and exposes its @route entries as MCP tools via JSON-RPC 2.0 over HTTP POST.

Mounts inside the server like any other app and participates in the full middleware chain (auth, CORS, errors). The index route handles all JSON-RPC messages via the default_entry mechanism.

Usage:

class SourcererMcpApp(McpApplication):
    mcp_name = "sourcerer"
    mcp_version = "1.0.0"

    def on_init(self, **kwargs):
        api = SourcererAPI(...)
        self.set_router(api.api)
__init__(**kwargs)[source]

Initialize with optional mcp_name/mcp_version overrides.

index()[source]

JSON-RPC 2.0 endpoint for MCP Streamable HTTP.

Reads the parsed body from request.data (populated by the framework via asgi_data). Dispatches based on JSON-RPC method.

For notifications (no “id” field), sets HTTP 202 and returns None. For requests, returns a JSON-RPC response dict.

Return type:

dict | None

mcp_name: str = 'genro-mcp'
mcp_version: str = '1.0.0'
set_router(router)[source]

Set the external genro-routes Router to expose as MCP tools.

Return type:

None