Setup
src/mcp/tool-index.ts
import { VectoriaDB, FileStorageAdapter, SerializationUtils, DocumentMetadata } from 'vectoriadb';
import { MCPServer, Tool } from 'frontmcp';
interface ToolDocument extends DocumentMetadata {
toolName: string;
owner: string;
tags: string[];
risk: 'safe' | 'destructive';
}
// Create VectoriaDB instance
const toolIndex = new VectoriaDB<ToolDocument>({
storageAdapter: new FileStorageAdapter({
cacheDir: './.cache/vectoriadb',
namespace: 'mcp-tools',
}),
defaultSimilarityThreshold: 0.4,
});
// Index MCP tools
export async function indexMCPTools(server: MCPServer) {
await toolIndex.initialize();
const tools = server.getTools();
const documents = tools.map((tool: Tool) => ({
id: tool.name,
text: `${tool.name}: ${tool.description}`,
metadata: {
id: tool.name,
toolName: tool.name,
owner: 'mcp',
tags: tool.tags || [],
risk: tool.annotations?.destructiveHint ? 'destructive' : 'safe',
},
}));
await toolIndex.addMany(documents);
await toolIndex.saveToStorage();
}
// Search for relevant tools
export async function findTools(query: string, options?: { filter?: (m: ToolDocument) => boolean }) {
return toolIndex.search(query, {
topK: 5,
threshold: 0.4,
filter: options?.filter,
});
}
Usage with FrontMCP Server
src/mcp/server.ts
import { MCPServer } from 'frontmcp';
import { indexMCPTools, findTools } from './tool-index';
const server = new MCPServer({
name: 'my-mcp-server',
});
// Register your tools
server.tool('users:list', 'List all users', async () => { /* ... */ });
server.tool('users:create', 'Create a new user', async () => { /* ... */ });
// Index tools for semantic search
await indexMCPTools(server);
// Use in tool discovery
const relevantTools = await findTools('find user accounts');
console.log('Suggested tools:', relevantTools.map(t => t.metadata.toolName));
Dynamic Tool Discovery
src/mcp/discovery.ts
// Add a discovery endpoint
server.tool('discover:tools', 'Find relevant tools for a query', async (params) => {
const { query, category } = params;
const tools = await findTools(query, {
filter: category ? (m) => m.tags.includes(category) : undefined,
});
return {
tools: tools.map(t => ({
name: t.metadata.toolName,
score: t.score,
description: t.text,
})),
};
});
Related
Tool Discovery Guide
Complete guide
Enclave
Enclave integration
Express
Express integration