Skip to main content
Glama
gemini2026

Documentation Search MCP Server

by gemini2026

get_code_examples

Find curated code examples for specific libraries and topics to implement programming features with clear explanations.

Instructions

Get curated code examples for a specific topic and library.

Args:
    library: The library to search for examples
    topic: The specific topic or feature
    language: Programming language for examples
    version: Library version to search (e.g., "4.2", "stable", "latest"). Default: "latest"
    auto_detect_version: Automatically detect installed package version. Default: False

Returns:
    Curated code examples with explanations

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
libraryYes
topicYes
languageNopython
versionNolatest
auto_detect_versionNo

Implementation Reference

  • The @mcp.tool()-decorated async function that implements the core logic of the 'get_code_examples' tool. It performs filtered searches for code examples using smart_search helpers, falls back to web search and regex extraction if needed, and returns structured examples with metadata. The function signature defines the input schema via type hints.
    async def get_code_examples(
        library: str,
        topic: str,
        language: str = "python",
        version: str = "latest",
        auto_detect_version: bool = False,
    ):
        """
        Get curated code examples for a specific topic and library.
    
        Args:
            library: The library to search for examples
            topic: The specific topic or feature
            language: Programming language for examples
            version: Library version to search (e.g., "4.2", "stable", "latest"). Default: "latest"
            auto_detect_version: Automatically detect installed package version. Default: False
    
        Returns:
            Curated code examples with explanations
        """
    
        await enforce_rate_limit("get_code_examples")
    
        # Enhanced query for code-specific search
        code_query = f"{library} {topic} example code {language}"
    
        try:
            # Use filtered search to find examples with code
            from .smart_search import filtered_search, SearchFilters
    
            filters = SearchFilters(content_type="example", has_code_examples=True)
    
            results = await filtered_search.search_with_filters(
                code_query, library, filters
            )
    
            if not results:
                # Fallback to regular search
                if library not in docs_urls:
                    return {"error": f"Library {library} not supported"}
    
                query = f"site:{docs_urls[library]} {code_query}"
                search_results = await search_web(query)
    
                if not search_results.get("organic"):
                    return {"error": "No code examples found"}
    
                # Process first result for code extraction
                first_result = search_results["organic"][0]
                content = await fetch_url(first_result["link"])
    
                # Extract code snippets (simplified)
                code_blocks = []
                import re
    
                code_pattern = r"```(?:python|javascript|typescript|js)?\n(.*?)```"
                matches = re.finditer(code_pattern, content, re.DOTALL)
    
                for i, match in enumerate(matches):
                    if i >= 3:  # Limit to 3 examples
                        break
                    code_blocks.append(
                        {
                            "example": i + 1,
                            "code": match.group(1).strip(),
                            "language": language,
                            "source_url": first_result["link"],
                        }
                    )
    
                return {
                    "library": library,
                    "topic": topic,
                    "language": language,
                    "total_examples": len(code_blocks),
                    "examples": code_blocks,
                }
    
            else:
                # Process enhanced results
                examples = []
                for i, result in enumerate(results[:3]):
                    examples.append(
                        {
                            "example": i + 1,
                            "title": result.title,
                            "description": (
                                result.snippet[:200] + "..."
                                if len(result.snippet) > 200
                                else result.snippet
                            ),
                            "url": result.url,
                            "difficulty": result.difficulty_level,
                            "estimated_read_time": f"{result.estimated_read_time} min",
                        }
                    )
    
                return {
                    "library": library,
                    "topic": topic,
                    "language": language,
                    "total_examples": len(examples),
                    "examples": examples,
                }
    
        except Exception as e:
            return {"error": f"Failed to get code examples: {str(e)}"}

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/gemini2026/documentation-search-mcp'

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