Skip to content

Local Unit Testing

Unlike other minimal frameworks, AegisMCP enforces strict context propagation for security and observability.

When the ExecutionPipeline runs your tools over a network, it automatically intercepts the request, verifies authentication, and injects a hardened AegisContext directly into your tool function.

If you want to bypass the server and directly test your tools locally in Python (for unit tests or debugging), you must manually construct the AegisContext yourself.

Manual Context Construction

Here is a complete example of how to instantiate a secure context to test a tool locally:

import asyncio
from datetime import datetime, timedelta, UTC
from aegismcp.server.app import AegisMCP
from aegismcp.kernel.context import AegisContext, Identity

app = AegisMCP("TestServer")

@app.tool()
async def say_hello(ctx: AegisContext, name: str) -> str:
    # Notice that we expect the context to be injected
    user_type = ctx.caller_identity.type
    return f"Hello {name}! You are executing as a {user_type}."

async def main():
    # 1. Manually construct the security context for local testing
    ctx = AegisContext(
        request_id="test-req-001",
        trace_id="trace-123",
        span_id="span-123",
        caller_identity=Identity(id="TestUser", type="developer"),
        permissions=frozenset(),
        deadline=datetime.now(UTC) + timedelta(minutes=5),
        metadata={},
        baggage={}
    )

    # 2. Execute the tool by passing the context
    result = await say_hello(ctx, name="Ujjwal")
    print(result)

if __name__ == "__main__":
    asyncio.run(main())

This pattern guarantees that tools written for AegisMCP are 100% strictly typed and can be reliably tested using standard Python pytest suites without spinning up network sockets.