/**
* detect_ai_music Tool
*
* Detects if audio was generated by AI or created by humans.
*/
import { Tool } from '@modelcontextprotocol/sdk/types.js';
import { AIDetectionResult } from '../types/mcp-tools.js';
import {
IRCAM_API_CONFIG,
IrcamAIDetectorResponse,
transformAIDetectorResponse,
} from '../types/ircam-api.js';
import { httpPost, buildApiUrl } from '../utils/http.js';
import { validateAudioUrl } from '../utils/validation.js';
import { formatError } from '../utils/errors.js';
/**
* Tool definition for MCP registration
*/
export const detectAiMusicTool: Tool = {
name: 'detect_ai_music',
description:
'Detect whether an audio file was generated by AI or created by humans. ' +
'Accepts a public URL to an audio file (MP3, WAV, FLAC, OGG, M4A). ' +
'Returns a confidence score (0-100) and classification (ai_generated, human_made, or uncertain).',
inputSchema: {
type: 'object',
properties: {
audio_url: {
type: 'string',
description: 'Public URL to the audio file to analyze',
},
},
required: ['audio_url'],
},
};
/**
* Handle detect_ai_music tool call
*/
export async function handleDetectAiMusic(
args: Record<string, unknown>
): Promise<AIDetectionResult> {
const audioUrl = args.audio_url as string;
// Validate input
const validation = validateAudioUrl(audioUrl);
if (!validation.valid) {
throw validation.error;
}
// Call IRCAM AI Detector API
const url = buildApiUrl(IRCAM_API_CONFIG.ENDPOINTS.AI_DETECTOR);
const response = await httpPost<IrcamAIDetectorResponse>(url, {
url: audioUrl,
});
if (!response.ok || !response.data) {
throw response.error || formatError('UNKNOWN_ERROR', 'Failed to detect AI music');
}
// Transform and return result
return transformAIDetectorResponse(response.data);
}