serve_static_files
serve_static_files
Serve static files (HTML, CSS, JS, images) from a local directory. Agent routes take priority over static files, so requests matching a registered agent route are handled by the agent first. Unmatched paths fall through to static file serving.
This method is useful for serving a web frontend alongside your agents — for example, an admin dashboard or a configuration UI.
Unlike mounting StaticFiles directly on the FastAPI app, this method integrates
correctly with agent route priority. Static files are served via the catch-all handler
that runs on startup.
Parameters
directory
Path to the directory containing static files. Must exist and be a valid directory.
Raises ValueError if the path does not exist or is not a directory.
route
URL path prefix for static files. Use "/" to serve from the root. Directory requests
automatically resolve to index.html if it exists.
Returns
None
Example
With this configuration:
Path traversal attempts (e.g., /../etc/passwd) are blocked. The resolved file path
must remain within the configured static directory.