Skip to content

feat(mcp): implement public API for package information#16

Merged
gsmlg merged 6 commits intomainfrom
008-mcp-public-api
Jan 28, 2026
Merged

feat(mcp): implement public API for package information#16
gsmlg merged 6 commits intomainfrom
008-mcp-public-api

Conversation

@gsmlg
Copy link
Contributor

@gsmlg gsmlg commented Jan 28, 2026

Summary

  • Implement public (unauthenticated) MCP API access for AI clients to query package information
  • Add IP-based rate limiting with Mnesia backend to protect against abuse
  • Fix critical data mapping issues in MCP tools (package/release struct field names)
  • Add comprehensive test coverage for all user stories (15 new tests)

Key Changes

Rate Limiting (lib/hex_hub/mcp/transport.ex)

  • Implement check_rate_limit/2 with Mnesia-based IP tracking
  • Use mcp:ip: key prefix for MCP-specific rate limits
  • Emit telemetry event [:hex_hub, :mcp, :rate_limited] for monitoring

Authentication Bypass (lib/hex_hub_web/controllers/mcp_controller.ex)

  • Return 429 with JSON-RPC error code -32002 and retry-after header when rate limited
  • Authentication passes through when require_auth: false

Data Mapping Fixes (lib/hex_hub/mcp/tools/packages.ex)

  • Fix package.repositorypackage.repository_name
  • Fix release.metadatarelease.meta with map/JSON string handling
  • Fix release.retirementrelease.retired with format conversion
  • Add helper functions: normalize_meta/1, format_retirement_info/1

Configuration (config/config.exs)

  • Document MCP_ENABLED, MCP_REQUIRE_AUTH, MCP_RATE_LIMIT environment variables
  • Add public deployment example

User Stories Implemented

  • US1: AI client queries package information (search, get, list)
  • US2: AI client lists available tools (tool discovery, schemas)
  • US3: AI client retrieves package dependencies (metadata, dependency tree)
  • US4: AI client accesses package documentation links

Test plan

  • Run mix test test/hex_hub_web/controllers/mcp_public_test.exs - 15 tests pass
  • Run full test suite mix test - 406 tests pass
  • Verify public access without API key works when require_auth: false
  • Verify rate limiting returns 429 with retry-after header
  • Verify MCP disabled returns 503 with error code -32001

- Implement check_rate_limit/2 in transport.ex with Mnesia-based IP tracking
- Add MCP-specific rate limit key prefix "mcp:ip:" for per-IP limits
- Configure rate limit window and track requests per IP per minute
- Return JSON-RPC error code -32002 with retry-after header when rate limited
- Add telemetry event [:hex_hub, :mcp, :rate_limited] for monitoring
- Implement rate_limit_mcp_request/2 plug in controller with 429 response
- Rate limiting blocks public deployment until complete (as per spec)
- Fix package.repository -> package.repository_name field reference
- Fix release.metadata -> release.meta with proper map/JSON string handling
- Fix release.retirement -> release.retired (boolean to retirement info map)
- Add normalize_meta/1 helper to handle both map and JSON string meta formats
- Add format_retirement_info/1 to convert boolean retired status to info map
- Update format_release/1 to use format_retirement_info(release.retired)
- Update get_meta_field/3 to handle both map and JSON string meta types
- Remove unused get_retirement_info/1 function
- Add 15 tests covering all user stories in mcp_public_test.exs
- User Story 1: Package queries without authentication (6 tests)
  - search_packages, get_package, list_packages returns data without auth
  - invalid JSON-RPC request handling
  - non-existent package error handling
- User Story 2: Tool discovery (3 tests)
  - GET /mcp/tools returns tools without authentication
  - GET /mcp/server-info returns capabilities with auth status
  - All tools include inputSchema for client discovery
- User Story 3: Dependencies (3 tests)
  - get_package_metadata returns dependencies without auth
  - get_dependency_tree returns structure without auth
  - check_compatibility works without authentication
- User Story 4: Documentation (2 tests)
  - Package info includes external links without auth
  - get_documentation tool accessible without authentication
- Cross-cutting concerns (2 tests)
  - Rate limiting test with IP-based tracking
  - MCP disabled returns error -32001 with 503 status
- Add MCP public API section to CLAUDE.md with configuration examples
- Document MCP_ENABLED, MCP_REQUIRE_AUTH, MCP_RATE_LIMIT settings
- Add public deployment example with environment variables
- Add rate limiting protection notes for public access deployments
- Add MCP endpoints list (/mcp/health, /mcp/tools, /mcp/server-info, /mcp)
- Update config/config.exs with enhanced MCP configuration comments
- Document public deployment example with recommended rate limit (100 req/hr)
- Add rate limiting rationale for abuse protection without authentication
- Add complete tasks.md with all 41 tasks marked complete across 7 phases
- Fix code formatting in mcp_public_test.exs, runtime.exs, users.ex
- Fix HEEx formatting in admin app layout (Phoenix 1.8 syntax)
- Update Dialyzer workflow to include OTP/Elixir version in cache key
- Add PLT build step when cache miss occurs
- Fix additional code formatting in mcp_public_test.exs (line breaking)
- Add dialyzer ignore for pattern_match_cov warning in packages.ex
  (defensive normalize_meta/1 clause for runtime safety)
@gsmlg gsmlg merged commit d0358bd into main Jan 28, 2026
6 checks passed
@gsmlg gsmlg deleted the 008-mcp-public-api branch January 28, 2026 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments