chat_mcp_server

AlandeXiong/chat_mcp_server

3.1

If you are the rightful owner of chat_mcp_server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

The Model Context Protocol (MCP) server is designed to facilitate communication between clients and language models, providing a structured and efficient way to manage interactions.

Campaign Journey MCP Server with Spring AI

A Spring Boot application that provides a complete MCP (Model Context Protocol) implementation with both client and server components, integrated with Spring AI for creating comprehensive marketing campaigns through chat-based interactions.

Architecture

The application is now structured with two main components:

1. MCP Server (McpServerConfig.java)

  • Provides MCP server functionality
  • Integrates with Spring AI ChatClient
  • Handles marketing campaign requests
  • Manages server properties and configuration

2. MCP Client (McpClientConfig.java)

  • Connects to MCP servers via SSE transport
  • Supports both synchronous and asynchronous clients
  • Provides tool callback integration
  • Customizable client behavior

Features

  • MCP Server Integration: Full MCP server implementation
  • MCP Client Support: Multiple client connections with customization
  • Spring AI Integration: Powered by Spring AI framework
  • Marketing Campaign Creation: Generate complete marketing campaigns
  • Target Audience Segmentation: Define and analyze target audiences
  • Channel Strategy Development: Create multi-channel marketing strategies
  • Email Template Generation: Generate email templates and content
  • WebSocket Communication: Real-time chat interface
  • REST API: HTTP endpoints for all operations
  • Tool Callbacks: MCP tool integration with Spring AI

Prerequisites

  • Java 17 or higher
  • Maven 3.6 or higher
  • OpenAI API key or Azure OpenAI credentials

Setup

  1. Clone the repository

    git clone <repository-url>
    cd campaign-journey-backend
    
  2. Set up your AI credentials

    Option 1: OpenAI

    export OPENAI_API_KEY=your-openai-api-key-here
    export OPENAI_MODEL=gpt-4
    

    Option 2: Azure OpenAI

    export AZURE_OPENAI_API_KEY=your-azure-api-key-here
    export AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com
    export AZURE_OPENAI_DEPLOYMENT_NAME=gpt-4
    
  3. Build the application

    mvn clean install
    
  4. Run the application

    mvn spring-boot:run
    

Configuration

MCP Server Configuration

spring:
  ai:
    mcp:
      server:
        enabled: true
        name: "Campaign Journey MCP Server"
        version: "1.0.0"
        description: "MCP Server for marketing campaign journey management using Spring AI"

MCP Client Configuration

spring:
  ai:
    mcp:
      client:
        enabled: true
        name: "campaign-journey-mcp-client"
        version: "1.0.0"
        type: SYNC  # or ASYNC
        request-timeout: 30s
        initialized: true
        root-change-notification: true
        toolcallback:
          enabled: true
        
        # SSE Transport
        sse:
          connections:
            local-server:
              url: http://localhost:8080
              sse-endpoint: /sse

Usage

WebSocket Connection

Connect to the WebSocket endpoint:

ws://localhost:8080/chat

MCP Commands

The WebSocket interface supports MCP-specific commands:

/mcp/status    - Check MCP server status
/mcp/tools     - Get available MCP tools
/mcp/health    - Check MCP health

Marketing Commands

"Create a marketing campaign for life insurance products"
"Define target audience for our insurance products"
"Develop a multi-channel marketing strategy"
"Generate an email template for promotion"
"Analyze campaign performance metrics"
"Optimize budget allocation across channels"

REST API Endpoints

Marketing Operations
  • POST /api/marketing/campaign - Create campaign
  • POST /api/marketing/audience - Define audience
  • POST /api/marketing/strategy - Develop strategy
  • POST /api/marketing/template - Generate template
  • POST /api/marketing/performance - Analyze performance
  • POST /api/marketing/optimization - Optimize budget
MCP Operations
  • GET /api/mcp/server/info - Get MCP server info
  • GET /api/mcp/server/health - Check MCP health
  • GET /api/mcp/clients - Get MCP client info
  • POST /api/mcp/request - Send MCP request
General
  • GET /api/capabilities - Get all capabilities

MCP Client Customization

The application provides customizable MCP clients:

Sync Client Customizer

@Component
public class CustomMcpSyncClientCustomizer implements McpSyncClientCustomizer {
    @Override
    public void customize(String serverConfigurationName, McpClient.SyncSpec spec) {
        spec.requestTimeout(Duration.ofSeconds(30));
        spec.toolsChangeConsumer((tools) -> {
            // Handle tools change
        });
        // ... more customization options
    }
}

Async Client Customizer

@Component
public class CustomMcpAsyncClientCustomizer implements McpAsyncClientCustomizer {
    @Override
    public void customize(String serverConfigurationName, McpClient.AsyncSpec spec) {
        spec.requestTimeout(Duration.ofSeconds(30));
    }
}

Transport Support

  • SSE (Server-Sent Events): HTTP-based transport for remote MCP servers
  • Stdio: Standard I/O transport for local MCP servers
  • WebFlux: Reactive transport support (optional)

Tool Callback Integration

The MCP client automatically integrates with Spring AI's tool execution framework:

@Autowired
private SyncMcpToolCallbackProvider toolCallbackProvider;

ToolCallback[] toolCallbacks = toolCallbackProvider.getToolCallbacks();

Response Structure

All responses include:

  • type: Response type (campaign, segment, strategy, etc.)
  • content: AI-generated content
  • status: Success/error status
  • timestamp: Response timestamp
  • Additional fields based on response type

Troubleshooting

Common Issues

  1. MCP Client Connection: Check SSE endpoint configuration
  2. AI Model Access: Verify API keys and model configuration
  3. Transport Issues: Ensure proper transport configuration

Logs

Enable debug logging:

logging:
  level:
    org.springframework.ai: DEBUG
    com.example.mcp: DEBUG
    org.springframework.ai.mcp: DEBUG

Development

Project Structure

src/main/java/com/example/mcp/
ā”œā”€ā”€ McpServerConfig.java        # MCP Server configuration
ā”œā”€ā”€ McpClientConfig.java        # MCP Client configuration
ā”œā”€ā”€ MarketingCampaignService.java    # Marketing business logic
ā”œā”€ā”€ MCPClientService.java       # MCP client operations
ā”œā”€ā”€ MCPWebSocketHandler.java    # WebSocket message handler
ā”œā”€ā”€ WebSocketConfig.java        # WebSocket configuration
ā”œā”€ā”€ MarketingCampaignController.java # REST API controller
└── McpServerApplication.java   # Spring Boot main class

Adding New Features

  1. New MCP Tools: Extend the MCP server with new tool definitions
  2. Client Customization: Add new client customizers for specific behavior
  3. Transport Support: Implement new transport mechanisms

License

This project is licensed under the MIT License.

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request