search_xml_content
Find specific records in Apple Health XML files by searching attribute values. The tool returns matching XML elements, limiting results to a specified maximum. It streams data for memory efficiency.
Instructions
Search for specific content in the Apple Health XML file and return matching records as XML text.
Parameters:
query: Text to search for in any attribute value
max_results: Maximum number of matching records to return (default: 50)
Returns:
A string containing up to max_results XML elements that match the query, or a message if no matches are found.
Notes for LLMs:
Searches both Record and Workout elements
Useful for finding all records containing a specific value, device, or type
This function streams the file for memory efficiency and does not load the entire file into memory.
If filename is not provided, the file set by set_xml_file will be used.
Do not guess, auto-fill, or assume any missing data.
When asked for medical advice, try to use my data from ElasticSearch first.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| max_results | No | ||
| query | No |
Implementation Reference
- app/mcp/v1/tools/xml_reader.py:37-63 (handler)FastMCP tool handler decorated with @tool. Wraps the core search_xml function with try-except error handling and detailed docstring describing usage.@xml_reader_router.tool def search_xml_content(query: str = "", max_results: int = 50) -> str: """ Search for specific content in the Apple Health XML file and return matching records as XML text. Parameters: - query: Text to search for in any attribute value - max_results: Maximum number of matching records to return (default: 50) Returns: - A string containing up to max_results XML elements that match the query, or a message if no matches are found. Notes for LLMs: - Searches both Record and Workout elements - Useful for finding all records containing a specific value, device, or type - This function streams the file for memory efficiency and does not load the entire file into memory. - If filename is not provided, the file set by set_xml_file will be used. - Do not guess, auto-fill, or assume any missing data. - When asked for medical advice, try to use my data from ElasticSearch first. """ try: return search_xml(query, max_results) except Exception as e: return f"Error searching XML content: {str(e)}"
- Core implementation of the XML content search. Streams the XML file using iterparse, searches Record and Workout elements for query matches in attributes, and formats results as XML strings.def search_xml(query: str = "", max_results: int = 50) -> str: results = [] query_lower = query.lower() for elem in stream_xml_elements(["Record", "Workout"]): matches = any(value and query_lower in value.lower() for value in elem.attrib.values()) if matches: elem_str = ET.tostring(elem, encoding="unicode", method="xml") results.append(elem_str) if len(results) >= max_results: break if not results: return f"No matches found for query: '{query}'" return ( f"""Search Results for '{query}' (showing up to {max_results} results):\n\n""" f"""{chr(10).join(results)}\n\nTotal matches found: {len(results)}""" )
- app/mcp/v1/mcp.py:3-10 (registration)Registers the xml_reader_router (which includes the search_xml_content tool) by mounting it onto the main mcp_router. Also imports the tools modules.from app.mcp.v1.tools import duckdb_reader, es_reader, xml_reader mcp_router = FastMCP(name="Main MCP") mcp_router.mount(duckdb_reader.duckdb_reader_router) # mcp_router.mount(ch_reader.ch_reader_router) mcp_router.mount(es_reader.es_reader_router) mcp_router.mount(xml_reader.xml_reader_router)
- app/main.py:9-11 (registration)Mounts the main mcp_router (containing xml_reader_router and thus search_xml_content) onto the root FastMCP application server.mcp = FastMCP(settings.PROJECT_NAME) mcp.mount(mcp_router)