| name | generate-mcp-handler-from-swagger |
| description | Generate MCP handler methods based on Swagger/OpenAPI specifications for Cloud.ru Container Apps |
Generate MCP Handler from Swagger
When to use this skill
Use this skill when you need to generate new MCP handler methods based on Swagger/OpenAPI specifications for Cloud.ru Container Apps service. This skill helps automate the creation of consistent handler code that follows the established patterns in the project.
Prerequisites
- Valid OpenAPI/Swagger specification for the Cloud.ru Container Apps API endpoint you want to implement
- Understanding of the existing MCP handler patterns in the project
Required Parameters
The following parameters are mandatory:
openapi_spec - The OpenAPI/Swagger specification (JSON/YAML format) for the endpoint
handler_name - Name for the new handler (e.g., "containerapp_create")
method_name - The HTTP method name from the spec (e.g., "CreateContainerApp")
Process Overview
- Validate the OpenAPI specification
- Extract relevant information from the spec:
- Operation ID
- Parameters and their types
- Request/Response schemas
- Required vs optional fields
- Generate the handler code following the established patterns
- Create appropriate domain types if needed
- Register the tool in the server
How to validate OpenAPI specification
- Ensure the specification is valid JSON or YAML
- Verify it contains the operation you want to implement
- Check that all required parameters and schemas are defined
- Confirm the specification matches the Cloud.ru Container Apps API structure
Generated Handler Structure
The generated handler will include:
Registration Function
func (s *MCPServer) Register[MethodName]Tool(mcpServer *server.MCPServer) {
}
Parameter Processing
- Extraction of required and optional parameters
- Type conversion (string to int, bool, etc.)
- Default value handling
- Error checking for required parameters
Service Call
- Creation of domain request structs
- Calling the appropriate service method
- Handling responses and errors
Response Formatting
- JSON marshaling of results
- Proper error handling and messaging
Examples
Sample Input OpenAPI Spec (YAML)
paths:
/projects/{projectId}/containerApps:
post:
operationId: CreateContainerApp
summary: Create a new Container App
parameters:
- name: projectId
in: path
required: true
schema:
type: string
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/CreateContainerAppRequest'
responses:
'200':
description: Successful response
content:
application/json:
schema:
$ref: '#/components/schemas/Operation'
Generated Handler (simplified)
func (s *MCPServer) RegisterCreateContainerAppTool(mcpServer *server.MCPServer) {
toolOptions := s.getMCPFieldsOptions(
"Create a new Container App in Cloud.ru",
"project_id",
"containerapp_name",
"containerapp_port",
)
createContainerAppTool := mcp.NewTool("cloudru_create_containerapp", toolOptions...)
mcpServer.AddTool(createContainerAppTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
projectID, err := s.getMCPFieldValue("project_id", request)
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
createRequest := domain.CreateContainerAppRequest{
ProjectID: projectID,
}
operation, err := s.containerAppsService.CreateContainerApp(createRequest)
if err != nil {
return mcp.NewToolResultError(err.Error()), nil
}
result, err := json.MarshalIndent(operation, "", " ")
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("Failed to format result: %v", err)), nil
}
return mcp.NewToolResultText(fmt.Sprintf("Successfully created Container App: %s\n%s", containerAppName, string(result))), nil
})
}
Important Notes
- The OpenAPI specification is REQUIRED - without it, the generation cannot proceed
- Generated code should be reviewed and adjusted as needed
- Ensure parameter names match the existing conventions in mappedFields
- Follow the existing patterns for error handling and response formatting
- Update the RegisterAllTools function to register the new handler