Skip to main content

EDteam API Integration

This example demonstrates how to build a production-ready MCP server in Go that integrates with a real-world API, handles authentication, and follows best practices.

Overview

The EDteam server showcases:
  • Authentication: Secure login and token management
  • HTTP Client: Reusable HTTP request handling
  • Multiple Endpoints: Integrating various API endpoints
  • Error Handling: Robust error handling patterns
  • Environment Variables: Secure credential management
  • Production Patterns: Real-world API integration strategies

Project Structure

Data Models

Subscription Model

models.go

Course Model

HTTP Client

Reusable Request Function

http.go
This utility function:
  • Handles both raw bytes and JSON marshaling
  • Sets appropriate headers
  • Manages authentication tokens
  • Includes context for cancellation
  • Properly closes response bodies

Authentication

Login Flow

login.go

API Integrations

Subscriptions

subscription.go

Courses

courses.go

Shopping Cart

shopping-cart.go

MCP Server Setup

Main Server

main.go

Tool: List Subscriptions

Tool: List Courses

Tool: Add to Shopping Cart

Environment Configuration

Setting Credentials

Claude Desktop Configuration

claude_desktop_config.json

Building and Running

Dependencies

go.mod

Build

Production Patterns

Error Handling

Always check status codes and return meaningful errors.

Context Propagation

Pass context through all API calls for proper cancellation and timeout handling.

Resource Cleanup

Always close response bodies to prevent resource leaks.

Type Safety

Use type assertions with fallback values.

Security Best Practices

  1. Environment Variables: Never hardcode credentials
  2. Token Management: Store tokens securely, not in logs
  3. HTTPS Only: Always use HTTPS for API calls
  4. Error Messages: Don’t leak sensitive information in errors
  5. Logging: Use os.Stderr for logs (not stdout)

Testing

Unit Test Example

Key Takeaways

  1. Reusable HTTP Client: Create utilities for common operations
  2. Authentication First: Handle auth before registering tools
  3. Struct Tags: Use JSON tags for proper serialization
  4. Error Wrapping: Use fmt.Errorf with %w for error chains
  5. Environment Config: Use env vars for sensitive data
  6. Context Usage: Always propagate context for cancellation

Next Steps