Skip to main content
Glama
spacemeowx2

Cargo Doc MCP Server

by spacemeowx2

list_symbols

List all symbols in a Rust crate to implement traits or explore available types, including structs, enums, and traits with their paths.

Instructions

List all symbols in a crate. Use when implementing traits or exploring available types. Shows structs, enums, traits with their paths.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
project_pathYesPath to the Rust project (must be absolute path)
crate_nameYesName of the crate to list symbols for

Implementation Reference

  • Core handler function that traverses the Rust documentation directory, parses symbol files, and returns a list of SymbolInfo objects including name, type, path, and URL.
    public async listSymbols(projectPath: string, crateName: string): Promise<SymbolInfo[]> {
        const isBuilt = await this.checkDoc(projectPath, crateName);
        if (!isBuilt) {
            throw new DocError(
                DocErrorCode.SEARCH_FAILED,
                'Failed to access documentation'
            );
        }
    
        const cached = await this.cache.get(projectPath, crateName);
        if (!cached) {
            throw new DocError(
                DocErrorCode.CACHE_ERROR,
                'Cache error: Documentation entry not found'
            );
        }
    
        try {
            const { docPath } = cached;
            const docDir = path.dirname(docPath);
            const symbols: SymbolInfo[] = [];
    
            // 定义符号收集处理函数
            const symbolHandler = async (fileName: string, filePath: string, modulePath: string) => {
                const symbol = this.parseSymbolFromFile(fileName, modulePath, crateName, filePath);
                if (symbol) {
                    symbols.push(symbol);
                }
            };
    
            // 使用通用的traverseDirectory收集符号
            await this.traverseDirectory(docDir, crateName, '', symbolHandler);
    
            return symbols.sort((a, b) => a.path.localeCompare(b.path));
        } catch (error) {
            throw new DocError(
                DocErrorCode.SEARCH_FAILED,
                'Failed to list symbols',
                error
            );
        }
    }
  • Input schema definition for the list_symbols tool, specifying project_path and crate_name as required string parameters.
    inputSchema: {
      type: "object",
      properties: {
        project_path: {
          type: "string",
          description: "Path to the Rust project (must be absolute path)",
        },
        crate_name: {
          type: "string",
          description: "Name of the crate to list symbols for",
        },
      },
      required: ["project_path", "crate_name"],
    },
  • src/index.ts:106-123 (registration)
    Tool registration in the ListTools response, defining name, description, and input schema.
    {
      name: "list_symbols",
      description: "List all symbols in a crate. Use when implementing traits or exploring available types. Shows structs, enums, traits with their paths.",
      inputSchema: {
        type: "object",
        properties: {
          project_path: {
            type: "string",
            description: "Path to the Rust project (must be absolute path)",
          },
          crate_name: {
            type: "string",
            description: "Name of the crate to list symbols for",
          },
        },
        required: ["project_path", "crate_name"],
      },
    },
  • Type definition for SymbolInfo, used as the return type of listSymbols.
    export interface SymbolInfo {
        name: string;
        type: SymbolType;
        path: string;
        url: string;
    }
  • Helper function to parse individual symbol information from documentation HTML file names.
    private parseSymbolFromFile(fileName: string, modulePath: string, crateName: string, filePath: string): SymbolInfo | null {
        const match = fileName.match(/^(struct|enum|trait|fn|const|type|macro|mod)\.(.+)\.html$/);
        if (!match) {
            return null;
        }
    
        const [, type, name] = match;
        const symbolName = name.replace(/-/g, '::');
        const fullPath = modulePath
            ? `${crateName}::${modulePath}::${symbolName}`
            : `${crateName}::${symbolName}`;
    
        return {
            name: symbolName,
            type: type as SymbolType,
            path: fullPath,
            url: RustdocUrl.create(filePath)
        };
    }
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/spacemeowx2/cargo-doc-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server