Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork31
🦀 Prevents outdated Rust code suggestions from AI assistants. This MCP server fetches current crate docs, uses embeddings/LLMs, and provides accurate context via a tool call.
License
Govcraft/rust-docs-mcp-server
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
⭐Like this project? Pleasestar the repository onGitHub to show your support and stay updated! ⭐
Modern AI-powered coding assistants (like Cursor, Cline, Roo Code, etc.) excelat understanding code structure and syntax but often struggle with the specificsof rapidly evolving libraries and frameworks, especially in ecosystems like Rustwhere crates are updated frequently. Their training data cutoff means they maylack knowledge of the latest APIs, leading to incorrect or outdated codesuggestions.
This MCP server addresses this challenge by providing a focused, up-to-dateknowledge source for a specific Rust crate. By running an instance of thisserver for a crate (e.g.,serde,tokio,reqwest), you give your LLM codingassistant a tool (query_rust_docs) it can usebefore writing code related tothat crate.
When instructed to use this tool, the LLM can ask specific questions about thecrate's API or usage and receive answers derived directly from thecurrentdocumentation. This significantly improves the accuracy and relevance of thegenerated code, reducing the need for manual correction and speeding updevelopment.
Multiple instances of this server can be run concurrently, allowing the LLMassistant to access documentation for several different crates during a codingsession.
This server fetches the documentation for a specified Rust crate, generatesembeddings for the content, and provides an MCP tool to answer questions aboutthe crate based on the documentation context.
- Targeted Documentation: Focuses on a single Rust crate per serverinstance.
- Feature Support: Allows specifying required crate features fordocumentation generation.
- Semantic Search: Uses OpenAI's
text-embedding-3-smallmodel to find themost relevant documentation sections for a given question. - LLM Summarization: Leverages OpenAI's
gpt-4o-mini-2024-07-18model togenerate concise answers basedonly on the retrieved documentation context. - Caching: Caches generated documentation content and embeddings in theuser's XDG data directory (
~/.local/share/rustdocs-mcp-server/or similar)based on crate, version,and requested features to speed up subsequentlaunches. - MCP Integration: Runs as a standard MCP server over stdio, exposing toolsand resources.
- OpenAI API Key: Needed for generating embeddings and summarizing answers.The server expects this key to be available in the
OPENAI_API_KEYenvironment variable. (The server also requires network access to downloadcrate dependencies and interact with the OpenAI API).
The recommended way to install is to download the pre-compiled binary for youroperating system from theGitHub Releases page.
- Go to theReleases page.
- Download the appropriate archive (
.zipfor Windows,.tar.gzforLinux/macOS) for your system. - Extract the
rustdocs_mcp_server(orrustdocs_mcp_server.exe) binary. - Place the binary in a directory included in your system's
PATHenvironmentvariable (e.g.,/usr/local/bin,~/bin).
If you prefer to build from source, you will need theRust Toolchain installed.
- Clone the repository:
git clone https://github.com/Govcraft/rust-docs-mcp-server.gitcd rust-docs-mcp-server - Build the server:
cargo build --release
Important Note for New Crates:
When using the server with a crate for the first time (or with a new version/feature set), it needs to download the documentation and generate embeddings. This process can take some time, especially for crates with extensive documentation, and requires an active internet connection and OpenAI API key.
It is recommended to run the server once directly from your command line for any new crate configurationbefore adding it to your AI coding assistant (like Roo Code, Cursor, etc.). This allows the initial embedding generation and caching to complete. Once you see the server startup messages indicating it's ready (e.g., "MCP Server listening on stdio"), you can shut it down (Ctrl+C). Subsequent launches, including those initiated by your coding assistant, will use the cached data and start much faster.
The server is launched from the command line and requires thePackage IDSpecification for the target crate. This specification follows the format usedby Cargo (e.g.,crate_name,crate_name@version_req). For the fullspecification details, seeman cargo-pkgid or theCargo documentation.
Optionally, you can specify required crate features using the-F or--features flag, followed by a comma-separated list of features. This isnecessary for crates that require specific features to be enabled forcargo doc to succeed (e.g., crates requiring a runtime feature likeasync-stripe).
# Set the API key (replace with your actual key)export OPENAI_API_KEY="sk-..."# Example: Run server for the latest 1.x version of serderustdocs_mcp_server"serde@^1.0"# Example: Run server for a specific version of reqwestrustdocs_mcp_server"reqwest@0.12.0"# Example: Run server for the latest version of tokiorustdocs_mcp_server tokio# Example: Run server for async-stripe, enabling a required runtime featurerustdocs_mcp_server"async-stripe@0.40" -F runtime-tokio-hyper-rustls# Example: Run server for another crate with multiple featuresrustdocs_mcp_server"some-crate@1.2" --features feat1,feat2
On the first run for a specific crate versionand feature set, the serverwill:
- Download the crate documentation using
cargo doc(with specified features). - Parse the HTML documentation.
- Generate embeddings for the documentation content using the OpenAI API (thismay take some time and incur costs, though typically only fractions of a USpenny for most crates; even a large crate like
async-stripewith over 5000documentation pages cost only $0.18 USD for embedding generation duringtesting). - Cache the documentation content and embeddings so that the cost isn'tincurred again.
- Start the MCP server.
Subsequent runs for the same crate versionand feature set will load the datafrom the cache, making startup much faster.
The server communicates using the Model Context Protocol over standardinput/output (stdio). It exposes the following:
Tool:
query_rust_docs- Description: Query documentation for the specific Rust crate the serverwas started for, using semantic search and LLM summarization.
- Input Schema:
{"type":"object","properties": {"question": {"type":"string","description":"The specific question about the crate's API or usage." } },"required": ["question"]} - Output: A text response containing the answer generated by the LLM basedon the relevant documentation context, prefixed with
From <crate_name> docs:. - Example MCP Call:
{"jsonrpc":"2.0","method":"callTool","params": {"tool_name":"query_rust_docs","arguments": {"question":"How do I make a simple GET request with reqwest?" } },"id":1}
Resource:
crate://<crate_name>- Description: Provides the name of the Rust crate this server instance isconfigured for.
- URI:
crate://<crate_name>(e.g.,crate://serde,crate://reqwest) - Content: Plain text containing the crate name.
Logging: The server sends informational logs (startup messages, queryprocessing steps) back to the MCP client via
logging/messagenotifications.
You can configure MCP clients like Roo Code to run multiple instances of thisserver, each targeting a different crate. Here's an example snippet for RooCode'smcp_settings.json file, configuring servers forreqwest andasync-stripe (note the added features argument forasync-stripe):
{"mcpServers": {"rust-docs-reqwest": {"command":"/path/to/your/rustdocs_mcp_server","args": ["reqwest@0.12" ],"env": {"OPENAI_API_KEY":"YOUR_OPENAI_API_KEY_HERE" },"disabled":false,"alwaysAllow": [] },"rust-docs-async-stripe": {"command":"rustdocs_mcp_server","args": ["async-stripe@0.40","-F"," runtime-tokio-hyper-rustls" ],"env": {"OPENAI_API_KEY":"YOUR_OPENAI_API_KEY_HERE" },"disabled":false,"alwaysAllow": [] } }}Note:
- Replace
/path/to/your/rustdocs_mcp_serverwith the actual path to thecompiled binary on your system if it isn't in your PATH. - Replace
YOUR_OPENAI_API_KEY_HEREwith your actual OpenAI API key. - The keys (
rust-docs-reqwest,rust-docs-async-stripe) are arbitrary namesyou choose to identify the server instances within Roo Code.
For Claude Desktop users, you can configure the server in the MCP settings.Here's an example configuring servers forserde andasync-stripe:
{"mcpServers": {"rust-docs-serde": {"command":"/path/to/your/rustdocs_mcp_server","args": ["serde@^1.0" ] },"rust-docs-async-stripe-rt": {"command":"rustdocs_mcp_server","args": ["async-stripe@0.40","-F","runtime-tokio-hyper-rustls" ] } }}Note:
- Ensure
rustdocs_mcp_serveris in your system's PATH or provide the full path(e.g.,/path/to/your/rustdocs_mcp_server). - The keys (
rust-docs-serde,rust-docs-async-stripe-rt) are arbitrary namesyou choose to identify the server instances. - Remember to set the
OPENAI_API_KEYenvironment variable where Claude Desktopcan access it (this might be system-wide or via how you launch ClaudeDesktop). Claude Desktop's MCP configuration might not directly supportsetting environment variables per-server like Roo Code. - The example shows how to add the
-Fargument for crates likeasync-stripethat require specific features.
- Location: Cached documentation and embeddings are stored in the XDG datadirectory, typically under
~/.local/share/rustdocs-mcp-server/<crate_name>/<sanitized_version_req>/<features_hash>/embeddings.bin.Thesanitized_version_reqis derived from the version requirement, andfeatures_hashis a hash representing the specific combination of featuresrequested at startup. This ensures different feature sets are cachedseparately. - Format: Data is cached using
bincodeserialization. - Regeneration: If the cache file is missing, corrupted, or cannot bedecoded, the server will automatically regenerate the documentation andembeddings.
- Initialization: Parses the crate specification and optional features fromthe command line using
clap. - Cache Check: Looks for a pre-existing cache file for the specific crate,version requirement, and feature set.
- 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 docusing thecargolibrary API to generate HTMLdocumentation in the temporary directory. - Dynamically locates the correct output directory within
target/docbysearching for the subdirectory containingindex.html.
- Creates a temporary Rust project depending only on the target crate,enabling the specified features in its
- Content Extraction (if cache miss):
- Walks the generated HTML files within the located documentation directory.
- Uses the
scrapercrate to parse each HTML file and extract text contentfrom the main content area (<section>).
- Embedding Generation (if cache miss):
- Uses the
async-openaicrate andtiktoken-rsto generate embeddings foreach extracted document chunk using thetext-embedding-3-smallmodel. - Calculates the estimated cost based on the number of tokens processed.
- Uses the
- Caching (if cache miss): Saves the extracted document content and theircorresponding embeddings to the cache file (path includes features hash)using
bincode. - Server Startup: Initializes the
RustDocsServerwith theloaded/generated documents and embeddings. - MCP Serving: Starts the MCP server using
rmcpover stdio. - Query Handling (
query_rust_docstool):- Generates an embedding for the user's question.
- Calculates the cosine similarity between the question embedding and allcached document embeddings.
- Identifies the document chunk with the highest similarity.
- Sends the user's question and the content of the best-matching documentchunk to the
gpt-4o-mini-2024-07-18model via the OpenAI API. - The LLM is prompted to answer the question basedonly on the providedcontext.
- Returns the LLM's response to the MCP client.
This project is licensed under the MIT License.
Copyright (c) 2025 Govcraft
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.
About
🦀 Prevents outdated Rust code suggestions from AI assistants. This MCP server fetches current crate docs, uses embeddings/LLMs, and provides accurate context via a tool call.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.