Skip to content

[Feature Request] Enhance AgentApp with Endpoint Registration, Middleware Support, and Lifecycle Hooks #72

@xuehuitian45

Description

@xuehuitian45

Description

Currently, deploying an AgentScope agent with AgentApp does not support cusrom endpoints and middleware, also, the lifecycle hook is also missing.

To improve developer experience, we propose enhancing the AgentApp class to support:

  1. Declarative endpoint registration (e.g., for /chat, /process)
  2. Middleware pipeline (for logging, auth, tracing, rate limiting, etc.)
  3. Application lifecycle hooks:
    • before_start(): run DB migrations, warm up models, connect to Redis
    • after_finish(): gracefully shutdown executors, flush metrics, close connections

This will allow users to define a production-ready agent service in just a few lines.


Proposed API

AgentApp app = new AgentApp(agent);

// Register middleware (executed on every request)
app.use(new LoggingMiddleware());
app.use(new TracingMiddleware());

// Register custom endpoints
app.post("/custom-action", MyCustomHandler);

// Lifecycle hooks
app.beforeStart(() -> {
    System.out.println("Warming up");
});

app.afterFinish(() -> {
    System.out.println("Shutting down...");
});

Or via builder pattern:

new AgentApp(agent)
    .middleware(new AuthMiddleware())
    .onBeforeStart(initVectorStore())
    .onAfterFinish(closeObservability())
    .post("/custom-action", MyCustomHandler);

Benefits

  • Reduce boilerplate for serving agents over HTTP
  • Standardize observability, auth, and error handling via middleware
  • Enable pre/post startup logic (critical for sandbox, RAG index, etc.)
  • Match Python runtime’s AgentApp ergonomics

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions