MCP Base
by jsmiff
- server
- resources
// Sample MCP Resource Template
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
/**
* Register sample resources with the MCP server
* @param server The MCP server instance
* @param supabase The Supabase client (optional)
*/
export function registerSampleResources(
server: McpServer,
supabase: any = null
) {
// List all items
server.resource("all-items", "items://all", async (uri) => {
try {
console.log("Fetching all items...");
// Example: Fetch data from a Supabase table if available
let data;
if (supabase) {
// const { data: fetchedData, error } = await supabase
// .from("items")
// .select("id, name, description");
//
// if (error) throw error;
// data = fetchedData;
} else {
console.log("Supabase not available, using mock data");
}
// For this template, let's return mock data
data = [
{ id: 1, name: "Item 1", description: "Description of item 1" },
{ id: 2, name: "Item 2", description: "Description of item 2" },
{ id: 3, name: "Item 3", description: "Description of item 3" },
];
console.log("Items fetched successfully");
return {
contents: [
{
uri: uri.href,
text: JSON.stringify(data, null, 2),
},
],
};
} catch (error) {
console.error("Error fetching items:", error);
throw error;
}
});
// Get item by ID
server.resource(
"item-by-id",
new ResourceTemplate("items://{id}", { list: undefined }),
async (uri, { id }) => {
try {
console.log(`Fetching item with ID: ${id}`);
// Example: Fetch data from a Supabase table by ID if available
let data;
if (supabase) {
// const { data: fetchedData, error } = await supabase
// .from("items")
// .select("*")
// .eq("id", id)
// .single();
//
// if (error) throw error;
// data = fetchedData;
} else {
console.log("Supabase not available, using mock data");
}
// For this template, let's return mock data
data = {
id: parseInt(id),
name: `Item ${id}`,
description: `Description of item ${id}`,
properties: {
color: "blue",
size: "medium",
tags: ["sample", "template", "example"]
}
};
console.log(`Item with ID ${id} fetched successfully`);
return {
contents: [
{
uri: uri.href,
text: JSON.stringify(data, null, 2),
},
],
};
} catch (error) {
console.error(`Error fetching item with ID ${id}:`, error);
throw error;
}
}
);
// Get items by category
server.resource(
"items-by-category",
new ResourceTemplate("items://category/{category}", { list: undefined }),
async (uri, { category }) => {
try {
console.log(`Fetching items for category: ${category}`);
// Example: Fetch data from a Supabase table by category if available
let data;
if (supabase) {
// const { data: fetchedData, error } = await supabase
// .from("items")
// .select("id, name, description")
// .eq("category", category);
//
// if (error) throw error;
// data = fetchedData;
} else {
console.log("Supabase not available, using mock data");
}
// For this template, let's return mock data
data = [
{ id: 1, name: `${category} Item 1`, description: `${category} description 1` },
{ id: 2, name: `${category} Item 2`, description: `${category} description 2` },
];
console.log(`Items for category ${category} fetched successfully`);
return {
contents: [
{
uri: uri.href,
text: JSON.stringify(data, null, 2),
},
],
};
} catch (error) {
console.error(`Error fetching items for category ${category}:`, error);
throw error;
}
}
);
}