import { HeimdallClient, traceMCPTool, observe } from 'hmdl';
// Configure via environment variables
process.env.HEIMDALL_ENDPOINT = "http://localhost:4318";
process.env.HEIMDALL_ORG_ID = "my-org";
process.env.HEIMDALL_PROJECT_ID = "my-project";
// Initialize client
const client = new HeimdallClient({
serviceName: "document-mcp-server",
environment: "production"
});
// Your MCP tools
const searchDocuments = traceMCPTool(
async (query: string, limit: number = 10) => {
const results = await db.search(query, limit);
return { results, count: results.length };
},
{ name: "search-documents", paramNames: ["query", "limit"] }
);
const getDocument = traceMCPTool(
async (docId: string) => {
const doc = await db.get(docId);
if (!doc) {
throw new Error(`Document ${docId} not found`);
}
return doc;
},
{ name: "get-document", paramNames: ["docId"] }
);
const createDocument = traceMCPTool(
async (title: string, content: string) => {
const doc = await db.create({ title, content });
return { id: doc.id, created: true };
},
{ name: "create-document", paramNames: ["title", "content"] }
);
// Internal helper with tracing
const validateQuery = observe(
(query: string): boolean => {
return query.length >= 3;
},
{ name: "validate-query" }
);
// Main execution
async function main() {
try {
// Run your MCP server logic
const result = await searchDocuments("test query", 5);
console.log(result);
} finally {
// Always flush before exit
await client.flush();
}
}
main();