Skip to content

[MCP] Server relies on process cwd for project discovery, causing "Nimsuggest is unavailable" when spawned from global config #536

Closed

Description

@Yakumo-Yukari

Nim 2.4.2

Nimlangserver 1.18.0

Description

When running nimlangserver --mcp via a global MCP client configuration, tool calls like nimCheckFile and nimCheckProject consistently fail with "Nimsuggest is unavailable".

Root Cause

In routes/mcp.nim (around line 577), the MCP server initializes nimsuggest instances strictly based on the process's current working directory (getCurrentDir()):

# routes/mcp.nim:577
let rootPath = getCurrentDir().pathToUri.uriToPath
ls.nimsuggestInit = ls.initNimsuggestInstances(rootPath)

When an MCP host spawns the server from a global config file (e.g., C:\Users\<username>\.gemini\config\mcp_config.json):

"nim": {
  "command": "nimlangserver",
  "args": ["--mcp"]
}

The process cwd defaults to the user's home directory (C:\Users\<username>) rather than the actual Nim project directory.

Because there is no .nimble file in the home directory, ls.initNimsuggestInstances(rootPath) finds zero projects (ls.projectFiles.len == 0). Later, when nimCheckFile or nimCheckProject is invoked, ls.tryGetNimsuggest(uri) fails to match any active instance and falls into the error branch at line 506:

# routes/mcp.nim:506
else:
  McpCallToolResult(content: @[McpContentBlock(text: "Nimsuggest is unavailable")], isError: true)

Steps to Reproduce

  1. Add nimlangserver --mcp to a global MCP client configuration without hardcoding a project-specific cwd.
  2. Open a Nim project with a .nimble file in a different directory.
  3. Trigger nimCheckFile or nimCheckProject via the MCP client.
  4. Observe the "Nimsuggest is unavailable" error.

Expected Behavior / Possible Fixes

nimlangserver in MCP mode shouldn't assume getCurrentDir() at startup is the target workspace. A few ways this could be handled:

  1. Lazy project resolution: When nimCheckFile(uri) is called and no matching nimsuggest instance exists, walk up the directory tree from the file's uri to locate the nearest .nimble file / project root, and initialize the nimsuggest instance on demand.
  2. Support MCP roots capability: Query workspace roots from the MCP client during initialization instead of falling back to getCurrentDir().
  3. CLI flag: Allow passing an explicit --project or --root path flag alongside --mcp as a fallback.

Activity

  1. nitely commented on Nov 17, 2026

    @nitely
    Collaborator

    Try v1.18.0; both APIs take a path/uri and the project is resolved similarly to fix 1. That code snippet you posted is from v1.16.0. If still fails, post your project layout and I'll try to reproduce it.

  2. Yakumo-Yukari commented on Nov 17, 2026

    @Yakumo-Yukari
    Author

    Try v1.18.0; both APIs take a path/uri and the project is resolved similarly to fix 1. That code snippet you posted is from v1.16.0. If still fails, post your project layout and I'll try to reproduce it.

    Thanks for the quick response, and apologies for the confusion! You are completely right—I inadvertently referenced the older v1.16.0 codebase when analyzing the error snippet.

    We have confirmed our environment is running nimlangserver 1.18.0 with Nim 2.4.2 on Windows:

    > nimlangserver --version
    1.18.0

    After testing further with v1.18.0:

    1. Lazy Resolution Works: We confirmed that the lazy project resolution via uri / path works properly—testing on a clean, isolated project returned diagnostics without needing cwd to be hardcoded to the workspace.
    2. Cold-start Timeout on Large Codebases: In our failing project (rika, a 2D game engine), the codebase is quite large (940 .nim files, ~230,000 lines of code):
      • nimble dump takes ~25–30 seconds to evaluate for this project.
      • nimsuggest then requires another 30–50 seconds to complete the initial semantic pass, consuming ~1.14 GB of RAM.
      • In cold-start conditions, total initialization time easily exceeds the 60-second NIMSUGGEST_STARTUP_TIMEOUT (or the MCP client request timeout), triggering an AsyncTimeoutError.
    3. Failure State Caching: Once the initial startup times out or fails, nimlangserver marks project.failed = true in memory. Subsequent MCP tool calls immediately return "Nimsuggest is unavailable" without retrying. (When we gave it a fresh session and ~80s of startup margin, nimsuggest did eventually start up and returned diagnostics successfully).

    Here is our project layout for reference:

    rika/
    ├── rika.nimble           # requires: sdl3_nim, zippy, nimcypher
    ├── nimble.paths
    ├── config.nims
    ├── src/
    │   ├── rika.nim          # main package entry point
    │   ├── rika.nim
    │   └── rika/             # ~940 files, ~230k LOC across submodules
    │       ├── gameplay/
    │       ├── graphics/
    │       ├── physics/
    │       └── renderfx/
    └── ...
    

    Thanks again for clarifying the v1.18.0 behavior! For large projects in MCP mode, it might be helpful if the startup timeout could be made configurable or if failed initialization could be retried on subsequent requests.

  3. nitely commented on Nov 19, 2026

    @nitely
    Collaborator

    nimble dump takes ~25–30 seconds to evaluate for this project.

    this would require the content of rika.nimble and maybe config.nims to reproduce. nimble dump should not take multiple seconds.

    the rest will be fixed in nimsuggest2 ; including removing the startup timeout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions