feat: expand MCP server functionality with customer, catalogue, marketing, and user tools and integrate new health indicators and configuration models.
This commit is contained in:
162
scaffold.py
Normal file
162
scaffold.py
Normal file
@@ -0,0 +1,162 @@
|
||||
import os
|
||||
|
||||
MCP_DIR = '/Users/sonphung/git/loyalty-agent-service/loyalty-mcp-server/src/main/java/dev/sonpx/loyalty/mcp'
|
||||
|
||||
domains = [
|
||||
('identity', 'User', 'UserResponseDto', '/api/user/csr/list', '/api/user/csr/id/{id}', 'identityHttpServiceProxyFactory'),
|
||||
('master', 'AppParam', 'AppParamDto', '/api/app-param/csr/list', '/api/app-param/csr/id/{id}', 'masterHttpServiceProxyFactory'),
|
||||
('marketing', 'Journey', 'JourneyDto', '/api/journey/csr/list', '/api/journey/csr/id/{id}', 'marketingHttpServiceProxyFactory'),
|
||||
('attribute', 'DynamicAttribute', 'DynamicAttributeDto', '/api/dynamic-attribute/csr/list', '/api/dynamic-attribute/csr/id/{id}', 'attributeHttpServiceProxyFactory')
|
||||
]
|
||||
|
||||
for domain, entity, dto, list_api, id_api, proxy_factory in domains:
|
||||
pkg = f"dev.sonpx.loyalty.mcp.{domain}"
|
||||
base_dir = os.path.join(MCP_DIR, domain)
|
||||
os.makedirs(base_dir, exist_ok=True)
|
||||
|
||||
# 1. Criteria
|
||||
criteria = f"""package {pkg}.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class {entity}Criteria {{
|
||||
private String search;
|
||||
private int page = 1;
|
||||
private int size = 10;
|
||||
}}
|
||||
"""
|
||||
with open(os.path.join(base_dir, 'model', f'{entity}Criteria.java'), 'w') as f:
|
||||
f.write(criteria)
|
||||
|
||||
# 2. Client
|
||||
client = f"""package {pkg}.service;
|
||||
|
||||
import dev.sonpx.loyalty.mcp.model.Result;
|
||||
import {pkg}.model.{dto};
|
||||
import {pkg}.model.{entity}Criteria;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.service.annotation.GetExchange;
|
||||
import org.springframework.web.service.annotation.HttpExchange;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@HttpExchange
|
||||
public interface {entity}Client {{
|
||||
|
||||
@GetExchange("{list_api}")
|
||||
Result<List<{dto}>> search(@ModelAttribute {entity}Criteria criteria);
|
||||
|
||||
@GetExchange("{id_api}")
|
||||
Result<{dto}> getById(@PathVariable("id") String id);
|
||||
}}
|
||||
"""
|
||||
os.makedirs(os.path.join(base_dir, 'service'), exist_ok=True)
|
||||
with open(os.path.join(base_dir, 'service', f'{entity}Client.java'), 'w') as f:
|
||||
f.write(client)
|
||||
|
||||
# 3. Service
|
||||
service = f"""package {pkg}.service;
|
||||
|
||||
import dev.sonpx.loyalty.mcp.model.Result;
|
||||
import {pkg}.model.{dto};
|
||||
import {pkg}.model.{entity}Criteria;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class {entity}Service {{
|
||||
|
||||
private final {entity}Client client;
|
||||
|
||||
public Result<List<{dto}>> search({entity}Criteria criteria) {{
|
||||
try {{
|
||||
return client.search(criteria);
|
||||
}} catch (Exception e) {{
|
||||
log.error("Failed to search {entity}", e);
|
||||
return Result.of(null, "FAILED", e.getMessage());
|
||||
}}
|
||||
}}
|
||||
|
||||
public Result<{dto}> getById(String id) {{
|
||||
try {{
|
||||
return client.getById(id);
|
||||
}} catch (Exception e) {{
|
||||
log.error("Failed to get {entity} by id", e);
|
||||
return Result.of(null, "FAILED", e.getMessage());
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
with open(os.path.join(base_dir, 'service', f'{entity}Service.java'), 'w') as f:
|
||||
f.write(service)
|
||||
|
||||
# 4. Tools
|
||||
tools = f"""package dev.sonpx.loyalty.mcp.tool;
|
||||
|
||||
import dev.sonpx.loyalty.mcp.model.Result;
|
||||
import {pkg}.model.{dto};
|
||||
import {pkg}.model.{entity}Criteria;
|
||||
import {pkg}.service.{entity}Service;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.ai.tool.annotation.Tool;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class {entity}Tools {{
|
||||
|
||||
private final {entity}Service service;
|
||||
|
||||
@Tool(description = "Search {entity} in the loyalty system")
|
||||
public Result<List<{dto}>> search{entity}(String search) {{
|
||||
{entity}Criteria criteria = new {entity}Criteria();
|
||||
criteria.setSearch(search);
|
||||
return service.search(criteria);
|
||||
}}
|
||||
|
||||
@Tool(description = "Get {entity} details by ID")
|
||||
public Result<{dto}> get{entity}ById(String id) {{
|
||||
return service.getById(id);
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
tools_dir = os.path.join(MCP_DIR, 'tool')
|
||||
with open(os.path.join(tools_dir, f'{entity}Tools.java'), 'w') as f:
|
||||
f.write(tools)
|
||||
|
||||
# 5. Client Bean Configuration
|
||||
config = f"""package {pkg}.config;
|
||||
|
||||
import {pkg}.service.{entity}Client;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
||||
|
||||
@Configuration
|
||||
public class {entity}Config {{
|
||||
|
||||
@Bean
|
||||
public {entity}Client {domain.lower()}Client(
|
||||
@Qualifier("{proxy_factory}") HttpServiceProxyFactory factory) {{
|
||||
return factory.createClient({entity}Client.class);
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
os.makedirs(os.path.join(base_dir, 'config'), exist_ok=True)
|
||||
with open(os.path.join(base_dir, 'config', f'{entity}Config.java'), 'w') as f:
|
||||
f.write(config)
|
||||
|
||||
print("Scaffold complete.")
|
||||
Reference in New Issue
Block a user