|
| 1 | +package io.github.jpicklyk.mcptask.application.tools.feature |
| 2 | + |
| 3 | +import io.github.jpicklyk.mcptask.application.tools.ToolCategory |
| 4 | +import io.github.jpicklyk.mcptask.application.tools.ToolExecutionContext |
| 5 | +import io.github.jpicklyk.mcptask.application.tools.ToolValidationException |
| 6 | +import io.github.jpicklyk.mcptask.application.tools.base.BaseToolDefinition |
| 7 | +import io.github.jpicklyk.mcptask.domain.model.EntityType |
| 8 | +import io.github.jpicklyk.mcptask.domain.rendering.MarkdownRenderer |
| 9 | +import io.github.jpicklyk.mcptask.domain.repository.RepositoryError |
| 10 | +import io.github.jpicklyk.mcptask.domain.repository.Result |
| 11 | +import io.github.jpicklyk.mcptask.infrastructure.util.ErrorCodes |
| 12 | +import io.modelcontextprotocol.kotlin.sdk.Tool |
| 13 | +import kotlinx.serialization.json.* |
| 14 | +import java.util.* |
| 15 | + |
| 16 | +/** |
| 17 | + * MCP tool for transforming a feature into markdown format with YAML frontmatter. |
| 18 | + * |
| 19 | + * This tool retrieves a feature and its sections, then renders them as a markdown document |
| 20 | + * with YAML frontmatter metadata. The markdown output is suitable for file export, |
| 21 | + * documentation generation, and systems that can render markdown directly. |
| 22 | + * |
| 23 | + * Use this tool when you need a markdown-formatted view of a feature rather than JSON data. |
| 24 | + * For inspecting feature details in structured format, use get_feature instead. |
| 25 | + * |
| 26 | + * Related tools: |
| 27 | + * - get_feature: To retrieve feature details in JSON format for inspection |
| 28 | + * - task_to_markdown: To transform a task into markdown |
| 29 | + * - project_to_markdown: To transform a project into markdown |
| 30 | + */ |
| 31 | +class FeatureToMarkdownTool : BaseToolDefinition() { |
| 32 | + override val category: ToolCategory = ToolCategory.FEATURE_MANAGEMENT |
| 33 | + |
| 34 | + override val name: String = "feature_to_markdown" |
| 35 | + |
| 36 | + override val title: String = "Transform Feature to Markdown" |
| 37 | + |
| 38 | + override val outputSchema: Tool.Output = Tool.Output( |
| 39 | + properties = JsonObject( |
| 40 | + mapOf( |
| 41 | + "success" to JsonObject(mapOf("type" to JsonPrimitive("boolean"))), |
| 42 | + "message" to JsonObject(mapOf("type" to JsonPrimitive("string"))), |
| 43 | + "data" to JsonObject( |
| 44 | + mapOf( |
| 45 | + "type" to JsonPrimitive("object"), |
| 46 | + "description" to JsonPrimitive("The feature rendered as markdown"), |
| 47 | + "properties" to JsonObject( |
| 48 | + mapOf( |
| 49 | + "markdown" to JsonObject( |
| 50 | + mapOf( |
| 51 | + "type" to JsonPrimitive("string"), |
| 52 | + "description" to JsonPrimitive("Markdown-formatted feature with YAML frontmatter") |
| 53 | + ) |
| 54 | + ), |
| 55 | + "featureId" to JsonObject( |
| 56 | + mapOf( |
| 57 | + "type" to JsonPrimitive("string"), |
| 58 | + "format" to JsonPrimitive("uuid") |
| 59 | + ) |
| 60 | + ) |
| 61 | + ) |
| 62 | + ) |
| 63 | + ) |
| 64 | + ), |
| 65 | + "error" to JsonObject(mapOf("type" to JsonArray(listOf(JsonPrimitive("object"), JsonPrimitive("null"))))), |
| 66 | + "metadata" to JsonObject(mapOf("type" to JsonPrimitive("object"))) |
| 67 | + ) |
| 68 | + ) |
| 69 | + ) |
| 70 | + |
| 71 | + override val description: String = """Transforms a feature into markdown format with YAML frontmatter. |
| 72 | +
|
| 73 | + This tool retrieves a feature and all its sections, then renders them as a markdown document |
| 74 | + with YAML frontmatter containing feature metadata. The output is suitable for: |
| 75 | + - File export and documentation generation |
| 76 | + - Systems that can render markdown directly |
| 77 | + - Version control and diff-friendly storage |
| 78 | + - Human-readable feature archives |
| 79 | +
|
| 80 | + The markdown output includes: |
| 81 | + - YAML frontmatter with feature metadata (id, name, status, priority, tags, dates) |
| 82 | + - Feature summary as the first content paragraph |
| 83 | + - All sections rendered according to their content format (markdown, code, JSON, plain text) |
| 84 | +
|
| 85 | + For inspecting feature details in structured JSON format, use get_feature instead. |
| 86 | + """ |
| 87 | + |
| 88 | + override val parameterSchema: Tool.Input = Tool.Input( |
| 89 | + properties = JsonObject( |
| 90 | + mapOf( |
| 91 | + "id" to JsonObject( |
| 92 | + mapOf( |
| 93 | + "type" to JsonPrimitive("string"), |
| 94 | + "description" to JsonPrimitive("The unique ID (UUID) of the feature to transform to markdown (e.g., '550e8400-e29b-41d4-a716-446655440000')") |
| 95 | + ) |
| 96 | + ) |
| 97 | + ) |
| 98 | + ), |
| 99 | + required = listOf("id") |
| 100 | + ) |
| 101 | + |
| 102 | + override fun validateParams(params: JsonElement) { |
| 103 | + // Validate required parameters |
| 104 | + val idStr = requireString(params, "id") |
| 105 | + try { |
| 106 | + UUID.fromString(idStr) |
| 107 | + } catch (_: IllegalArgumentException) { |
| 108 | + throw ToolValidationException("Invalid id format. Must be a valid UUID") |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + override suspend fun execute(params: JsonElement, context: ToolExecutionContext): JsonElement { |
| 113 | + logger.info("Executing feature_to_markdown tool") |
| 114 | + |
| 115 | + try { |
| 116 | + // Extract parameters |
| 117 | + val idStr = requireString(params, "id") |
| 118 | + val featureId = UUID.fromString(idStr) |
| 119 | + |
| 120 | + // Get the feature |
| 121 | + val featureResult = context.featureRepository().getById(featureId) |
| 122 | + |
| 123 | + return when (featureResult) { |
| 124 | + is Result.Success -> { |
| 125 | + val feature = featureResult.data |
| 126 | + |
| 127 | + // Get sections for the feature |
| 128 | + val sectionsResult = context.sectionRepository().getSectionsForEntity(EntityType.FEATURE, featureId) |
| 129 | + val sections = if (sectionsResult is Result.Success) sectionsResult.data else emptyList() |
| 130 | + |
| 131 | + // Render to markdown |
| 132 | + val renderer = MarkdownRenderer() |
| 133 | + val markdown = renderer.renderFeature(feature, sections) |
| 134 | + |
| 135 | + val data = buildJsonObject { |
| 136 | + put("markdown", markdown) |
| 137 | + put("featureId", featureId.toString()) |
| 138 | + } |
| 139 | + |
| 140 | + successResponse(data, "Feature transformed to markdown successfully") |
| 141 | + } |
| 142 | + |
| 143 | + is Result.Error -> { |
| 144 | + if (featureResult.error is RepositoryError.NotFound) { |
| 145 | + errorResponse( |
| 146 | + message = "Feature not found", |
| 147 | + code = ErrorCodes.RESOURCE_NOT_FOUND, |
| 148 | + details = "No feature exists with ID $featureId" |
| 149 | + ) |
| 150 | + } else { |
| 151 | + errorResponse( |
| 152 | + message = "Failed to retrieve feature", |
| 153 | + code = ErrorCodes.DATABASE_ERROR, |
| 154 | + details = featureResult.error.toString() |
| 155 | + ) |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } catch (e: ToolValidationException) { |
| 160 | + logger.warn("Validation error in feature_to_markdown: ${e.message}") |
| 161 | + return errorResponse( |
| 162 | + message = e.message ?: "Validation error", |
| 163 | + code = ErrorCodes.VALIDATION_ERROR |
| 164 | + ) |
| 165 | + } catch (e: Exception) { |
| 166 | + logger.error("Error transforming feature to markdown", e) |
| 167 | + return errorResponse( |
| 168 | + message = "Failed to transform feature to markdown", |
| 169 | + code = ErrorCodes.INTERNAL_ERROR, |
| 170 | + details = e.message ?: "Unknown error" |
| 171 | + ) |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments