server.tsā¢2.99 kB
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
// Import tools
import { listCollections, listCollectionsInputSchema } from "./tools/list-collections";
import { listAssets, listAssetsInputSchema } from "./tools/list-assets";
import { createCollection, createCollectionInputSchema } from "./tools/create-collection";
import { createAsset, createAssetInputSchema } from "./tools/create-asset";
import TOOLS from "../tools-config.json";
import packageJson from "../package.json";
// Create server instance
const server = new Server(
{
name: packageJson.name,
title: packageJson.title,
version: packageJson.version,
},
{
capabilities: {
tools: {},
},
},
);
// List tools handler
server.setRequestHandler(ListToolsRequestSchema, async () => {
return { tools: TOOLS };
});
// Call tool handler
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
switch (name) {
case "list_collections": {
const result = await listCollections();
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "list_assets": {
// Validate and parse arguments
const validatedArgs = listAssetsInputSchema.parse(args || {});
const result = await listAssets(validatedArgs);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "create_collection": {
// Validate and parse arguments
const validatedArgs = createCollectionInputSchema.parse(args);
const result = await createCollection(validatedArgs);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
case "create_asset": {
// Validate and parse arguments
const validatedArgs = createAssetInputSchema.parse(args);
const result = await createAsset(validatedArgs);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
default:
throw new Error(`Unknown tool: ${name}`);
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
return {
content: [
{
type: "text",
text: JSON.stringify(
{
success: false,
error: errorMessage,
},
null,
2,
),
},
],
isError: true,
};
}
});
export { server };