import os
from hmdl import HeimdallClient, trace_mcp_tool, observe
# Configure via environment variables
os.environ["HEIMDALL_ENDPOINT"] = "http://localhost:4318"
os.environ["HEIMDALL_ORG_ID"] = "my-org"
os.environ["HEIMDALL_PROJECT_ID"] = "my-project"
# Initialize client
client = HeimdallClient(
service_name="document-mcp-server",
environment="production"
)
# Your MCP tools
@trace_mcp_tool()
def search_documents(query: str, limit: int = 10) -> dict:
"""Search for documents."""
results = db.search(query, limit)
return {"results": results, "count": len(results)}
@trace_mcp_tool()
def get_document(doc_id: str) -> dict:
"""Retrieve a specific document."""
doc = db.get(doc_id)
if not doc:
raise ValueError(f"Document {doc_id} not found")
return doc
@trace_mcp_tool()
def create_document(title: str, content: str) -> dict:
"""Create a new document."""
doc = db.create(title=title, content=content)
return {"id": doc.id, "created": True}
# Internal helper with tracing
@observe(name="validate-query")
def validate_query(query: str) -> bool:
return len(query) >= 3
# Main execution
if __name__ == "__main__":
try:
# Run your MCP server logic
result = search_documents("test query", limit=5)
print(result)
finally:
# Always flush before exit
client.flush()