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 explanationsInput Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| library | Yes | ||
| topic | Yes | ||
| language | No | python | |
| version | No | latest | |
| auto_detect_version | No |
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)}"}