Skip to content

Type to search extensions

fastapi-lsp

v0.1.1

Language server for FastAPI and Starlette

Updated 4 months ago
489 downloads 1 star Repository ↗

fastapi-lsp

CI Release

A language server for FastAPI and Starlette. It understands routes, the Depends() graph, url_for, Jinja templates and env/settings files. It is a single Rust binary that works with any LSP-capable editor and reads your code statically, so it never imports or runs the app.

Features

Diagnostics param mismatches, shadowed and duplicate routes, unincluded routers, Depends(fn()), dependency cycles, broken url_for, missing templates, undefined env keys, includes placed before routes, prefixes without a leading slash or with a trailing one, path parameter markers, duplicate operation IDs, settings fields missing from env files
Navigation test call to handler, Depends() chains in both directions, template files, .env lines
Hover route card with resolved path, router chain, status code, summary, description, response model, dependencies and middleware
Completions route paths, url_for names, template paths, env keys, plus snippet stubs for routes, Depends() and url_for when the client supports snippets
Symbols GET /api/books/{book_id} in the symbol picker
Code lenses test count, dependency usage and override count per handler, route count per model
Code actions add a missing parameter, rename a parameter to match the path, convert to Annotated, extract a named dependency or router, create a dependency or model, fix template names
Linked editing {item_id} in a path and the handler's item_id parameter are edited together
Rename route name= (updates url_for calls and template references) and path parameters (also updates url_for keyword arguments). Other symbols, including dependency functions, return no rename, so pyright handles them
Call hierarchy incoming and outgoing calls over the Depends() graph
Highlight route name and url_for sites, and a path parameter with its handler parameter, within one file
Implementation a dependency and its dependency_overrides sites
File renames renaming a template rewrites TemplateResponse("x.html") and {% extends %} / {% include %} references (workspace/willRenameFiles)
check CLI the same diagnostics as a linter: fastapi-lsp check ., with --fix for the safe fixes

Diagnostics are push-only. The server sends them with textDocument/publishDiagnostics, does not offer pull diagnostics, and republishes a file only when its diagnostics change. If the client cannot register file watchers dynamically, the server watches the project itself.

Files under ignored directories such as .venv or site-packages are treated as read-only library code when you open them. They get no diagnostics or lenses.

Installation

uv tool install fastapi-lsp

Or with pip:

pip install fastapi-lsp

You can also download a pre-built binary from the releases page.

Editor setup

Neovim

vim.lsp.config('fastapi_lsp', {
  cmd = { 'fastapi-lsp', 'lsp' },
  filetypes = { 'python', 'html', 'htmldjango' },
  root_markers = { 'pyproject.toml', 'fastapi-lsp.toml', '.git' },
  -- init_options = { entrypoint = 'app/main.py', source_roots = { 'src' }, features = { code_lens = false } },
})
vim.lsp.enable('fastapi_lsp')

-- Code lenses and inlay hints are opt-in in Neovim.
vim.lsp.codelens.enable(true)
vim.lsp.inlay_hint.enable(true)

Neovim 0.12 has no document link UI, so document links do not show up there.

Helix

# ~/.config/helix/languages.toml
[language-server.fastapi-lsp]
command = "fastapi-lsp"
args = ["lsp"]

[[language]]
name = "python"
language-servers = ["pyright", "fastapi-lsp"]

[[language]]
name = "html"
language-servers = ["vscode-html-language-server", "fastapi-lsp"]

[[language]]
name = "jinja"
language-servers = ["fastapi-lsp"]

Pyright comes first, so its hover and goto stay primary in Python files and fastapi-lsp's are not used there. Helix has no code lens or document link UI, so those features do not show up.

Zed

Install the extension from the Zed extensions panel (Cmd+Shift+X): search for fastapi-lsp and click Install. The extension only declares the server, and Zed starts a non-default language server only for languages that list it. Add it to each language you want in ~/.config/zed/settings.json. The "..." entry keeps Zed's default servers running:

{
  "languages": {
    "Python": { "language_servers": ["pyright", "fastapi-lsp", "..."] },
    "HTML": { "language_servers": ["fastapi-lsp", "..."] },
    "Jinja2": { "language_servers": ["fastapi-lsp", "..."] }
  }
}

Without this, the server never starts.

For Python, list your regular language server before fastapi-lsp. The example uses pyright; use basedpyright, pylsp, ty or whatever you run. Zed sends rename, document highlight and linked editing requests only to the first listed server that advertises them. If fastapi-lsp came first, it would hide your Python server's rename and highlights. With the order above, fastapi-lsp still provides what other servers lack: diagnostics, route and path completion, code lenses and code actions. For HTML and Jinja2 keep fastapi-lsp first, since no other server competes for those features there.

To turn off fastapi-lsp's symbol-level features (rename, highlight, linked editing, call hierarchy, implementation, go to definition and references), set "features": { "navigation": false } in the initialization options. That also turns off route and dependency navigation, so the ordering above is usually the better fix.

The HTML and Jinja2 entries let the server index template files, so url_for calls and template references inside them resolve. The server does not filter by language on its own.

Configure the server under lsp.fastapi-lsp.initialization_options. The settings key is not read:

{
  "lsp": {
    "fastapi-lsp": {
      "initialization_options": { "templates": ["app/templates"] }
    }
  }
}

Zed turns code lenses and inlay hints off by default. Enable them to see the lenses and hints:

{
  "code_lens": "on",
  "inlay_hints": { "enabled": true }
}

Configuration

Standard projects need no configuration. When the same option is set in several places, the order of priority is InitializationOptions, then fastapi-lsp.toml, then [tool.fastapi-lsp] in pyproject.toml.

Option Default
entrypoint (auto-detected) main app file
templates [] Jinja template directories
source_roots [] extra import resolution roots
env_files [".env", ".env.example"] env key definitions
settings_env_files [".env", ".env.example", ".env.unittest"] env files checked for BaseSettings coverage
process_env false include server process env
process_env_show_values false show process-env values in hover
client_fixtures ["client", "async_client"] pytest HTTP client fixture names
env.ignore [] env keys to suppress from diagnostics
scan.ignore [".beads", ".claude", ".codegraph", ".git", ".mypy_cache", ".nox", ".playwright-mcp", ".pytest_cache", ".ruff_cache", ".tox", ".venv", "__pycache__", "build", "dist", "env", "node_modules", "site-packages", "target", "venv"] directory names excluded from workspace scans (replaces the default list instead of extending it)

Every feature toggle is true by default except test_unknown_paths:

Toggle Default
diagnostics true
completion true
hover true
navigation true
code_actions true
code_lens true
symbols true
inlay_hints true
document_links true
test_unknown_paths false

Initialization options use the same keys. For example, features = { code_lens = false } turns code lenses off.

# fastapi-lsp.toml
entrypoint = "app/main.py"
templates = ["app/templates"]

[features]
code_lens = false

[check]
only = []
ignore = ["env/undefined-key"]

[scan]
ignore = [".beads", ".claude", ".codegraph", ".git", ".mypy_cache", ".nox", ".playwright-mcp", ".pytest_cache", ".ruff_cache", ".tox", ".venv", "__pycache__", "build", "dist", "env", "node_modules", "site-packages", "target", "venv", ".worktrees"]

[check] sets the default only and ignore code lists for the check command. Passing --only or --ignore on the command line replaces both config lists.

CLI

fastapi-lsp lsp [--tcp --address 127.0.0.1 --port 9257]
fastapi-lsp check [PATH ...] [--only CODES] [--ignore CODES] [--format text|json] [--fix]
fastapi-lsp routes [PATH] [--format text|json]

fastapi-lsp lsp serves over stdio by default. Pass --tcp to serve over TCP instead.

check accepts several paths, so it works as a pre-commit hook. Each file is analysed against its own project root. With --fix it applies only fixes that keep the file valid Python and leaves the finding reported when it can't be sure.

Development

cargo build
cargo test
uv run --group dev pytest e2e/ -v

License

MIT