All files / web/src/app/api/mcp route.ts

0% Statements 0/380
0% Branches 0/1
0% Functions 0/1
0% Lines 0/380

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
/**
 * MCP (Model Context Protocol) API endpoint
 *
 * Handles JSON-RPC 2.0 requests for external tools like Claude Code.
 * Requires API key authentication via Authorization header.
 *
 * Supported methods:
 * - initialize: Capability negotiation
 * - tools/list: List available tools
 * - tools/call: Execute a tool
 * - resources/list: List available resources
 * - resources/read: Read a specific resource
 */

import { NextResponse } from 'next/server'
import { validateMcpApiKey } from '@/lib/mcp/auth'
import { listResources, readResource } from '@/lib/mcp/resources'
import {
  MCP_TOOLS,
  listStudents,
  verifyPlayerAccess,
  getStudentOverview,
  getSkills,
  getRecentErrors,
  getPracticeSessions,
  getRecommendedFocus,
  startPracticeSession,
  getActiveSession,
  controlSession,
  createObservationLink,
  listObservationLinks,
  generateWorksheet,
  getWorksheetInfo,
  listDifficultyProfiles,
} from '@/lib/mcp/tools'
import type { ShareDuration } from '@/lib/session-share'

const MCP_PROTOCOL_VERSION = '2025-03-26'

interface JsonRpcRequest {
  jsonrpc: '2.0'
  id: number | string
  method: string
  params?: Record<string, unknown>
}

interface JsonRpcResponse {
  jsonrpc: '2.0'
  id: number | string | null
  result?: unknown
  error?: {
    code: number
    message: string
    data?: unknown
  }
}

function jsonRpcError(
  id: number | string | null,
  code: number,
  message: string,
  data?: unknown
): JsonRpcResponse {
  return {
    jsonrpc: '2.0',
    id,
    error: { code, message, data },
  }
}

function jsonRpcSuccess(id: number | string | null, result: unknown): JsonRpcResponse {
  return {
    jsonrpc: '2.0',
    id,
    result,
  }
}

/**
 * POST /api/mcp - Handle MCP JSON-RPC requests
 */
export async function POST(request: Request) {
  // Validate MCP protocol version header
  const protocolVersion = request.headers.get('MCP-Protocol-Version')
  // Accept requests without version header for compatibility

  // Validate Authorization header
  const authHeader = request.headers.get('Authorization')
  const apiKey = authHeader?.replace('Bearer ', '')

  if (!apiKey) {
    return NextResponse.json(jsonRpcError(null, -32000, 'Missing Authorization header'), {
      status: 401,
    })
  }

  const userId = await validateMcpApiKey(apiKey)
  if (!userId) {
    return NextResponse.json(jsonRpcError(null, -32000, 'Invalid or revoked API key'), {
      status: 401,
    })
  }

  // Parse JSON-RPC request
  let body: JsonRpcRequest
  try {
    body = await request.json()
  } catch {
    return NextResponse.json(jsonRpcError(null, -32700, 'Parse error: Invalid JSON'), {
      status: 400,
    })
  }

  // Validate JSON-RPC structure
  if (body.jsonrpc !== '2.0' || !body.method) {
    return NextResponse.json(jsonRpcError(body.id ?? null, -32600, 'Invalid JSON-RPC request'), {
      status: 400,
    })
  }

  const { id, method, params = {} } = body

  try {
    // Handle MCP methods
    switch (method) {
      case 'initialize': {
        return NextResponse.json(
          jsonRpcSuccess(id, {
            protocolVersion: MCP_PROTOCOL_VERSION,
            capabilities: {
              tools: {},
              resources: {},
            },
            serverInfo: {
              name: 'abaci-one',
              version: '1.0.0',
            },
          })
        )
      }

      case 'tools/list': {
        return NextResponse.json(
          jsonRpcSuccess(id, {
            tools: MCP_TOOLS,
          })
        )
      }

      case 'tools/call': {
        const toolName = params.name as string
        const toolArgs = (params.arguments as Record<string, unknown>) || {}

        if (!toolName) {
          return NextResponse.json(jsonRpcError(id, -32602, 'Missing tool name'))
        }

        // Execute tool
        const result = await executeTool(userId, toolName, toolArgs)
        return NextResponse.json(
          jsonRpcSuccess(id, {
            content: [
              {
                type: 'text',
                text: JSON.stringify(result, null, 2),
              },
            ],
            isError: false,
          })
        )
      }

      case 'resources/list': {
        const result = listResources()
        return NextResponse.json(jsonRpcSuccess(id, result))
      }

      case 'resources/read': {
        const uri = params.uri as string
        if (!uri) {
          return NextResponse.json(jsonRpcError(id, -32602, 'Missing resource URI'))
        }

        const result = readResource(uri)
        if ('error' in result) {
          return NextResponse.json(jsonRpcError(id, -32002, result.error))
        }

        return NextResponse.json(jsonRpcSuccess(id, result))
      }

      default: {
        return NextResponse.json(jsonRpcError(id, -32601, `Method not found: ${method}`))
      }
    }
  } catch (error) {
    console.error(`MCP error in ${method}:`, error)
    const message = error instanceof Error ? error.message : 'Internal error'
    return NextResponse.json(
      jsonRpcSuccess(id, {
        content: [
          {
            type: 'text',
            text: JSON.stringify({ error: message }),
          },
        ],
        isError: true,
      })
    )
  }
}

/**
 * Execute an MCP tool with authorization checks
 */
async function executeTool(
  userId: string,
  toolName: string,
  args: Record<string, unknown>
): Promise<unknown> {
  // Worksheet tools don't require player access
  const worksheetToolNames = [
    'generate_worksheet',
    'get_worksheet_info',
    'list_difficulty_profiles',
  ]
  if (worksheetToolNames.includes(toolName)) {
    return executeWorksheetTool(toolName, args)
  }

  // Tools that don't require a player_id
  if (toolName === 'list_students') {
    return listStudents(userId)
  }

  // All other tools require player_id
  const playerId = args.player_id as string
  if (!playerId) {
    throw new Error('player_id is required')
  }

  // Verify user has access to this player
  const hasAccess = await verifyPlayerAccess(userId, playerId)
  if (!hasAccess) {
    throw new Error('Access denied to this player')
  }

  switch (toolName) {
    case 'get_student_overview':
      return getStudentOverview(playerId)

    case 'get_skills':
      return getSkills(
        playerId,
        args.status as string | undefined,
        args.category as string | undefined
      )

    case 'get_recent_errors':
      return getRecentErrors(
        playerId,
        (args.days as number) ?? 7,
        args.skill_id as string | undefined,
        (args.limit as number) ?? 20
      )

    case 'get_practice_sessions':
      return getPracticeSessions(playerId, (args.days as number) ?? 14)

    case 'get_recommended_focus':
      return getRecommendedFocus(playerId, (args.count as number) ?? 5)

    // Session management tools
    case 'start_practice_session':
      return startPracticeSession(playerId, userId, {
        durationMinutes: args.duration_minutes as number,
        autoStart: args.auto_start as boolean | undefined,
        enabledParts: args.enabled_parts as
          | { abacus?: boolean; visualization?: boolean; linear?: boolean }
          | undefined,
        maxTerms: args.max_terms as number | undefined,
        gameBreaks: args.game_breaks as
          | {
              enabled?: boolean
              maxMinutes?: number
              selectionMode?: 'auto-start' | 'kid-chooses'
            }
          | undefined,
      })

    case 'get_active_session':
      return getActiveSession(playerId)

    case 'control_session':
      return controlSession(
        playerId,
        args.session_id as string,
        args.action as 'approve' | 'start' | 'end_early' | 'abandon'
      )

    case 'create_observation_link':
      return createObservationLink(
        playerId,
        args.session_id as string,
        userId,
        args.expires_in as ShareDuration
      )

    case 'list_observation_links':
      return listObservationLinks(playerId, args.session_id as string)

    default:
      throw new Error(`Unknown tool: ${toolName}`)
  }
}

/**
 * Execute worksheet tools that don't require player access
 */
async function executeWorksheetTool(
  toolName: string,
  args: Record<string, unknown>
): Promise<unknown> {
  console.log(`[MCP] executeWorksheetTool: ${toolName}`, JSON.stringify(args, null, 2))
  switch (toolName) {
    case 'generate_worksheet':
      return generateWorksheet({
        operator: args.operator as 'addition' | 'subtraction' | 'mixed' | undefined,
        digitRange: args.digit_range as { min: number; max: number } | undefined,
        problemsPerPage: args.problems_per_page as number | undefined,
        pages: args.pages as number | undefined,
        difficultyProfile: args.difficulty_profile as string | undefined,
        scaffolding: args.scaffolding as
          | {
              carryBoxes?: 'always' | 'whenRegrouping' | 'whenMultipleRegroups' | 'never'
              answerBoxes?: 'always' | 'whenRegrouping' | 'whenMultipleRegroups' | 'never'
              placeValueColors?:
                | 'always'
                | 'whenRegrouping'
                | 'whenMultipleRegroups'
                | 'when3PlusDigits'
                | 'never'
              tenFrames?: 'always' | 'whenRegrouping' | 'whenMultipleRegroups' | 'never'
              borrowNotation?: 'always' | 'whenRegrouping' | 'never'
              borrowingHints?: 'always' | 'whenRegrouping' | 'whenMultipleRegroups' | 'never'
            }
          | undefined,
        showProblemNumbers: args.show_problem_numbers as boolean | undefined,
        progressiveDifficulty: args.progressive_difficulty as boolean | undefined,
        includeAnswerKey: args.include_answer_key as boolean | undefined,
        title: args.title as string | undefined,
        orientation: args.orientation as 'portrait' | 'landscape' | undefined,
        cols: args.cols as number | undefined,
      })

    case 'get_worksheet_info':
      return getWorksheetInfo(args.share_id as string)

    case 'list_difficulty_profiles':
      return listDifficultyProfiles()

    default:
      return null // Not a worksheet tool
  }
}

/**
 * OPTIONS - Handle CORS preflight
 */
export async function OPTIONS() {
  return new NextResponse(null, {
    status: 200,
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'POST, OPTIONS',
      'Access-Control-Allow-Headers':
        'Content-Type, Authorization, MCP-Protocol-Version, MCP-Session-Id',
    },
  })
}