MCP Base
by jsmiff
- server
- tools
// Sample MCP Tool Template
import { z } from "zod";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
/**
* Register a sample tool with the MCP server
* @param server The MCP server instance
* @param textGenerator The text generation service
* @param embeddings The embeddings service
* @param supabase The Supabase client (optional)
*/
export function registerSampleTool(
server: McpServer,
textGenerator: any,
embeddings: any,
supabase: any = null
) {
server.tool(
"sample-tool", // Tool name
{
// Tool parameters with validation using Zod
query: z.string().describe("The query string"),
maxResults: z.number().optional().describe("Maximum number of results to return"),
options: z.object({
filterBy: z.string().optional().describe("Optional filter criteria"),
sortBy: z.string().optional().describe("Optional sort criteria"),
}).optional().describe("Optional parameters"),
},
async ({ query, maxResults = 10, options = {} }) => {
try {
console.log(`Executing sample tool with query: "${query}"`);
// Your tool implementation goes here
// For example:
// 1. Generate embeddings for the query
// const embedding = await embeddings.generateEmbedding(query);
// 2. Perform vector search in Supabase (if available)
let results = [];
if (supabase) {
// const { data, error } = await supabase.rpc(
// "match_documents",
// {
// query_embedding: embedding,
// match_threshold: 0.7,
// match_count: maxResults,
// }
// );
// if (error) throw error;
// results = data || [];
} else {
console.log("Supabase not available, using mock data");
}
// For this template, let's return mock data
results = [
{ id: 1, title: "Sample result 1", score: 0.95 },
{ id: 2, title: "Sample result 2", score: 0.85 },
{ id: 3, title: "Sample result 3", score: 0.75 },
];
// You can apply optional filters
if (options.filterBy) {
console.log(`Filtering results by: ${options.filterBy}`);
// Implement filtering logic here
}
// You can apply optional sorting
if (options.sortBy) {
console.log(`Sorting results by: ${options.sortBy}`);
// Implement sorting logic here
}
console.log(`Found ${results.length} results for query: "${query}"`);
// Return the results
return {
content: [
{
type: "text",
text: JSON.stringify({
query,
results,
count: results.length,
options,
}, null, 2),
},
],
};
} catch (error) {
console.error("Error in sample tool:", error);
return {
content: [
{
type: "text",
text: JSON.stringify({
error: error.message,
}, null, 2),
},
],
isError: true,
};
}
}
);
}