Labsco
Govcraft logo

Rust Docs MCP Server

β˜… 283

from Govcraft

Query up-to-date documentation for Rust crates.

πŸ”₯πŸ”₯πŸ”₯βœ“ VerifiedAccount requiredAdvanced setup

Rust Docs MCP Server

License: MIT

⭐ Like this project? Please star the repository on GitHub to show your support and stay updated! ⭐

Motivation

Modern AI-powered coding assistants (like Cursor, Cline, Roo Code, etc.) excel at understanding code structure and syntax but often struggle with the specifics of rapidly evolving libraries and frameworks, especially in ecosystems like Rust where crates are updated frequently. Their training data cutoff means they may lack knowledge of the latest APIs, leading to incorrect or outdated code suggestions.

This MCP server addresses this challenge by providing a focused, up-to-date knowledge source for a specific Rust crate. By running an instance of this server for a crate (e.g., serde, tokio, reqwest), you give your LLM coding assistant a tool (query_rust_docs) it can use before writing code related to that crate.

When instructed to use this tool, the LLM can ask specific questions about the crate's API or usage and receive answers derived directly from the current documentation. This significantly improves the accuracy and relevance of the generated code, reducing the need for manual correction and speeding up development.

Multiple instances of this server can be run concurrently, allowing the LLM assistant to access documentation for several different crates during a coding session.

This server fetches the documentation for a specified Rust crate, generates embeddings for the content, and provides an MCP tool to answer questions about the crate based on the documentation context.

Features

  • Targeted Documentation: Focuses on a single Rust crate per server instance.
  • Feature Support: Allows specifying required crate features for documentation generation.
  • Semantic Search: Uses OpenAI's text-embedding-3-small model to find the most relevant documentation sections for a given question.
  • LLM Summarization: Leverages OpenAI's gpt-4o-mini-2024-07-18 model to generate concise answers based only on the retrieved documentation context.
  • Caching: Caches generated documentation content and embeddings in the user's XDG data directory (~/.local/share/rustdocs-mcp-server/ or similar) based on crate, version, and requested features to speed up subsequent launches.
  • MCP Integration: Runs as a standard MCP server over stdio, exposing tools and resources.

How it Works

  1. Initialization: Parses the crate specification and optional features from the command line using clap.
  2. Cache Check: Looks for a pre-existing cache file for the specific crate, version requirement, and feature set.
  3. Documentation Generation (if cache miss):
    • Creates a temporary Rust project depending only on the target crate, enabling the specified features in its Cargo.toml.
    • Runs cargo doc using the cargo library API to generate HTML documentation in the temporary directory.
    • Dynamically locates the correct output directory within target/doc by searching for the subdirectory containing index.html.
  4. Content Extraction (if cache miss):
    • Walks the generated HTML files within the located documentation directory.
    • Uses the scraper crate to parse each HTML file and extract text content from the main content area (<section id="main-content">).
  5. Embedding Generation (if cache miss):
    • Uses the async-openai crate and tiktoken-rs to generate embeddings for each extracted document chunk using the text-embedding-3-small model.
    • Calculates the estimated cost based on the number of tokens processed.
  6. Caching (if cache miss): Saves the extracted document content and their corresponding embeddings to the cache file (path includes features hash) using bincode.
  7. Server Startup: Initializes the RustDocsServer with the loaded/generated documents and embeddings.
  8. MCP Serving: Starts the MCP server using rmcp over stdio.
  9. Query Handling (query_rust_docs tool):
    • Generates an embedding for the user's question.
    • Calculates the cosine similarity between the question embedding and all cached document embeddings.
    • Identifies the document chunk with the highest similarity.
    • Sends the user's question and the content of the best-matching document chunk to the gpt-4o-mini-2024-07-18 model via the OpenAI API.
    • The LLM is prompted to answer the question based only on the provided context.
    • Returns the LLM's response to the MCP client.

License

This project is licensed under the MIT License.

Copyright (c) 2025 Govcraft

Sponsor

Govcraft is a one-person shopβ€”no corporate backing, no investors, just me building useful tools. If this project helps you, sponsoring keeps the work going.

Sponsor on GitHub