piyush1683/Spring-trade-mcp-server
If you are the rightful owner of Spring-trade-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 Spring Trade MCP Server is a Spring Boot application that provides trade operations and AI-powered analysis through both REST API and MCP server interfaces.
Spring Trade MCP Server
A Spring Boot application that exposes trade operations through both REST API and MCP (Model Context Protocol) server, enhanced with Spring AI capabilities for intelligent trade analysis and insights.
Features
Core Trade Operations
- Get All Trades: Retrieve all trades from the database
- Get Trade by ID: Fetch specific trade details
- Create Trade: Create new trade entries
- Trade Statistics: Get aggregated trade statistics
AI-Powered Analysis
- Trade Analysis: AI-powered insights on trading patterns and trends
- Recommendations: Get AI recommendations for specific trade types
- Trend Prediction: Predict future trading trends based on historical data
- Combined Insights: Comprehensive analysis combining multiple AI perspectives
Dual Interface
- REST API: Traditional HTTP endpoints for trade operations and AI analysis
- MCP Server: WebSocket-based Model Context Protocol server for real-time communication
Technology Stack
- Spring Boot 3.4.5: Core framework
- Spring AI: AI capabilities with OpenAI and Ollama support
- Spring WebSocket: Real-time communication
- PostgreSQL: Database
- Flyway: Database migration
- Gradle: Build tool
Prerequisites
- Java 21: Required for Spring Boot 3.x
- PostgreSQL: Database server
- Ollama (optional): For local AI processing
- OpenAI API Key (optional): For cloud-based AI processing
Setup Instructions
1. Database Setup
Create a PostgreSQL database:
CREATE DATABASE ibandb;
2. AI Configuration
Option A: Ollama (Local AI)
Install Ollama and run a model:
# Install Ollama
curl -fsSL https://ollama.ai/install.sh | sh
# Pull and run a model
ollama pull llama2
ollama run llama2
Option B: OpenAI (Cloud AI)
Uncomment and configure OpenAI settings in application.properties
:
spring.ai.openai.api-key=your-openai-api-key
spring.ai.openai.base-url=https://api.openai.com
spring.ai.openai.chat.options.model=gpt-3.5-turbo
3. Application Configuration
Update application.properties
with your database credentials:
spring.datasource.url=jdbc:postgresql://localhost:5432/ibandb
spring.datasource.username=your-username
spring.datasource.password=your-password
4. Build and Run
# Build the project
./gradlew build
# Run the application
./gradlew bootRun
The application will start on http://localhost:8081
API Endpoints
REST API
Trade Operations
GET /trades
- Get all tradesGET /trade/{tradeId}
- Get trade by IDPOST /trade
- Create new trade
AI Analysis
GET /api/ai/analyze
- Analyze all tradesGET /api/ai/recommendations?tradeType={type}
- Get recommendations for trade typeGET /api/ai/predict
- Predict trading trendsGET /api/ai/insights
- Get combined AI insights
MCP Server
The MCP server is available at WebSocket endpoint: /mcp
Trade Operations (MCP)
/app/trades/getAll
- Get all trades/app/trades/getById
- Get trade by ID/app/trades/create
- Create new trade/app/trades/statistics
- Get trade statistics
AI Operations (MCP)
/app/ai/analyze
- Analyze trades/app/ai/recommendations
- Get recommendations/app/ai/predict
- Predict trends/app/ai/insights
- Get combined insights
MCP Protocol
The MCP server uses JSON-RPC 2.0 protocol over WebSocket:
Request Format
{
"jsonrpc": "2.0",
"id": "1",
"method": "/trades/getAll",
"params": {}
}
Response Format
{
"jsonrpc": "2.0",
"id": "1",
"result": {
"trades": [...],
"count": 5
}
}
Testing the MCP Server
Web Client
Access the built-in test client at: http://localhost:8081/mcp-client.html
JavaScript Client Example
// Connect to MCP server
const socket = new SockJS('/mcp');
const stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
// Subscribe to responses
stompClient.subscribe('/topic/trades', function (response) {
const message = JSON.parse(response.body);
console.log('Trade response:', message);
});
// Send request
const request = {
jsonrpc: "2.0",
id: "1",
method: "/trades/getAll",
params: {}
};
stompClient.send("/app/trades/getAll", {}, JSON.stringify(request));
});
Python Client Example
import websocket
import json
# Connect to MCP server
ws = websocket.create_connection("ws://localhost:8081/mcp/websocket")
# Send request
request = {
"jsonrpc": "2.0",
"id": "1",
"method": "/trades/getAll",
"params": {}
}
ws.send(json.dumps(request))
# Receive response
response = ws.recv()
print(json.loads(response))
AI Capabilities
Trade Analysis
The AI service provides:
- Pattern recognition in trading data
- Risk assessment
- Optimization recommendations
- Market analysis based on trade types
Recommendations
- Optimal trade strategies
- Risk management approaches
- Market opportunities
- Best practices for specific trade types
Trend Prediction
- Future trading volume trends
- Expected trade type distribution
- Potential market movements
- Risk factors to watch
Configuration
Application Properties
Key configuration options in application.properties
:
# AI Configuration
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.options.model=llama2
# MCP Server Configuration
mcp.server.enabled=true
mcp.server.websocket.path=/mcp
mcp.server.topics.trades=/topic/trades
mcp.server.topics.ai=/topic/ai
Development
Project Structure
src/main/java/com/sample/trade/
āāā controller/ # REST controllers
āāā mcp/ # MCP server components
āāā ai/ # AI services
āāā service/ # Business logic
āāā repository/ # Data access
āāā command/ # DTOs and commands
Adding New MCP Methods
- Add method to
TradeMcpService
orAiMcpService
- Add corresponding endpoint in
McpController
orAiMcpController
- Update client documentation
Troubleshooting
Common Issues
- Database Connection: Ensure PostgreSQL is running and credentials are correct
- AI Service: Check if Ollama is running or OpenAI API key is configured
- WebSocket Connection: Verify the MCP endpoint is accessible at
/mcp
Logs
Enable debug logging by adding to application.properties
:
logging.level.com.sample.trade=DEBUG
logging.level.org.springframework.web=DEBUG
License
This project is licensed under the MIT License.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request