get_related_documents
Find related documents by providing a source file path from Magento 2 GraphQL documentation.
Instructions
Find documents related to the specified document
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | File path of source document |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- The handler function that implements the 'get_related_documents' tool logic. It queries the SQLite database for documents related by category/subcategory and by keyword matching, then returns formatted results.
@mcp.tool( name="get_related_documents", description="Find documents related to the specified document" ) def get_related_documents( file_path: Annotated[str, Field(description="File path of source document")] ) -> str: """Find related docs""" db = Database(DB_PATH) # Get source document try: source_doc = dict(db.query( "SELECT * FROM documents WHERE file_path = ?", [file_path] ).__next__()) except StopIteration: return f"Document not found: {file_path}" # Find related documents # 1. Same category and subcategory related_by_category = list(db.query( "SELECT * FROM documents WHERE category = ? AND subcategory = ? AND file_path != ? LIMIT 5", [source_doc['category'], source_doc['subcategory'], file_path] )) # 2. Similar keywords source_keywords = set(json.loads(source_doc.get('keywords_json', '[]'))) related_by_keywords = [] if source_keywords: for keyword in list(source_keywords)[:3]: matches = list(db.query( "SELECT * FROM documents WHERE keywords_json LIKE ? AND file_path != ? LIMIT 3", [f"%{keyword}%", file_path] )) related_by_keywords.extend(matches) # Combine and deduplicate seen = set() all_related = [] for doc in related_by_category + related_by_keywords: if doc['file_path'] not in seen: seen.add(doc['file_path']) all_related.append(doc) all_related = all_related[:5] if not all_related: return f"No related documents found for: {file_path}" # Format results lines = [f"# Related Documents for: {source_doc['title']}", ""] for doc in all_related: relationship = "Same category" if doc['category'] == source_doc['category'] else "Similar content" lines.append(f"### {doc['title']}") lines.append(f"**Path:** {doc['file_path']}") lines.append(f"**Relationship:** {relationship}") lines.append(f"**Category:** {doc['category']}/{doc.get('subcategory', 'N/A')}") if doc.get('description'): lines.append(f"**Description:** {doc['description'][:150]}...") lines.append("") return "\n".join(lines) - Input schema for the tool: requires a single 'file_path' string parameter.
def get_related_documents( file_path: Annotated[str, Field(description="File path of source document")] - magento_graphql_docs_mcp/server.py:429-432 (registration)Registration of the tool using FastMCP's @mcp.tool decorator with the name 'get_related_documents'.
@mcp.tool( name="get_related_documents", description="Find documents related to the specified document" ) - The DB_PATH configuration constant used by the handler to connect to the SQLite database.
DB_PATH = get_db_path() DOCS_PATH = get_docs_path()