index.ts•3.88 kB
#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import dotenv from 'dotenv';
import { NounProjectAPI } from './api.js';
import { TOOLS } from './tools.js';
// Load environment variables
dotenv.config();
// Validate required environment variables
const API_KEY = process.env.NOUN_PROJECT_API_KEY;
const API_SECRET = process.env.NOUN_PROJECT_API_SECRET;
if (!API_KEY || !API_SECRET) {
console.error('Error: NOUN_PROJECT_API_KEY and NOUN_PROJECT_API_SECRET must be set');
process.exit(1);
}
// Initialize API client
const api = new NounProjectAPI(API_KEY, API_SECRET);
// Create MCP server
const server = new Server(
{
name: 'noun-project-mcp',
version: '1.0.0',
},
{
capabilities: {
tools: {},
},
}
);
// Handle list tools request
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: TOOLS,
};
});
// Handle tool call requests
server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
const { name, arguments: args } = request.params;
switch (name) {
case 'search_icons': {
const result = await api.searchIcons(args as any);
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2),
},
],
};
}
case 'get_icon': {
const result = await api.getIcon(args as any);
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2),
},
],
};
}
case 'get_collection': {
const result = await api.getCollection(args as any);
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2),
},
],
};
}
case 'search_collections': {
const result = await api.searchCollections(args as any);
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2),
},
],
};
}
case 'icon_autocomplete': {
const result = await api.autocomplete(args as any);
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2),
},
],
};
}
case 'check_usage': {
const result = await api.checkUsage();
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2),
},
],
};
}
case 'get_download_url': {
const result = await api.getDownloadUrl(args as any);
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: `Error: ${errorMessage}`,
},
],
isError: true,
};
}
});
// Start the server
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
// Log to stderr to avoid interfering with stdio communication
console.error('Noun Project MCP server running on stdio');
}
main().catch((error) => {
console.error('Fatal error in main():', error);
process.exit(1);
});