search_examples
Search for Magento 2 GraphQL code examples by topic and filter by language—GraphQL, JSON, JavaScript, PHP, or bash—to find relevant snippets quickly.
Instructions
Search for code examples by topic and language
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search term for code examples | |
| language | No | Filter by language: graphql, json, javascript, php, bash |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- The main handler function for the search_examples tool. Accepts query (required) and language (optional) parameters. Queries the code_blocks table with a LIKE search on code and context columns, optionally filtered by language, limited to 10 results. Returns formatted Markdown with title, file path, language, context, and code preview.
@mcp.tool( name="search_examples", description="Search for code examples by topic and language" ) def search_examples( query: Annotated[str, Field(description="Search term for code examples")], language: Annotated[ Optional[str], Field(description="Filter by language: graphql, json, javascript, php, bash") ] = None ) -> str: """Search code blocks""" db = Database(DB_PATH) # Search in code and context sql = """ SELECT cb.*, d.title, d.file_path FROM code_blocks cb JOIN documents d ON cb.document_id = d.id WHERE (cb.code LIKE ? OR cb.context LIKE ?) """ params = [f"%{query}%", f"%{query}%"] if language: sql += " AND cb.language = ?" params.append(language) sql += " LIMIT 10" results = list(db.query(sql, params)) if not results: return f"No code examples found matching: {query}" # Format results formatted = [] for block in results: context_str = f"**Context:** {block['context']}\n" if block.get('context') else "" formatted.append( f"### {block['title']}\n" f"**File:** {block['file_path']}\n" f"**Language:** {block['language']}\n" f"{context_str}" f"\n```{block['language']}\n" f"{block['code'][:MAX_CODE_PREVIEW_LENGTH]}\n" # Limit code length for readability f"```\n" ) return "\n---\n\n".join(formatted) - Input schema for search_examples: a required 'query' string and optional 'language' string filter (graphql, json, javascript, php, bash).
query: Annotated[str, Field(description="Search term for code examples")], language: Annotated[ Optional[str], Field(description="Filter by language: graphql, json, javascript, php, bash") ] = None - magento_graphql_docs_mcp/server.py:377-379 (registration)Registration decorator that registers the function as an MCP tool named 'search_examples' with the FastMCP server instance.
@mcp.tool( name="search_examples", description="Search for code examples by topic and language" - Configuration constant limiting code preview length (default 400 characters), used in the handler to truncate displayed code.
MAX_CODE_PREVIEW_LENGTH = int(os.environ.get("MAGENTO_GRAPHQL_DOCS_CODE_PREVIEW", "400")) - Database path configuration used by the handler to open the sqlite-utils Database connection.
DB_PATH = get_db_path()