index.js•2.15 kB
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import player from 'play-sound';
import path from 'path';
import { fileURLToPath } from 'url';
// Get the directory name of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const audioPlayer = player();
// Use the custom sound file from the sounds directory
const COMPLETION_SOUND = path.join(__dirname, '..', 'sounds', 'completion.mp3');
// Set up a proper logging function that uses stderr
const log = (message) => {
process.stderr.write(`${message}\n`);
};
async function main() {
const server = new McpServer({
name: 'CursorSoundMCP',
version: '1.0.0'
});
// Tool to play sound after code generation
server.tool('playCompletionSound', 'Plays a completion sound when called', async () => {
try {
// Play custom sound using a Promise to handle the callback properly
await new Promise((resolve, reject) => {
audioPlayer.play(COMPLETION_SOUND, (err) => {
if (err) {
log(`Error playing sound: ${err.message}`);
reject(err);
}
else {
log('Played completion sound successfully');
resolve();
}
});
});
// Return proper JSON response
return {
content: [{ type: 'text', text: 'Played completion sound' }]
};
}
catch (error) {
log(`Failed to play sound: ${error}`);
return {
content: [{ type: 'text', text: 'Failed to play sound' }]
};
}
});
// Start the server using stdio transport
const transport = new StdioServerTransport();
await server.connect(transport);
log('Cursor Sound MCP server started...');
}
main().catch(error => {
log(`Failed to start MCP server: ${error}`);
process.exit(1);
});