-
Notifications
You must be signed in to change notification settings - Fork 118
Add McpActionBuilder class #1716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
guidorc
wants to merge
5
commits into
master
Choose a base branch
from
mcp-action-builder
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
712971e
Add McpActionBuilder class
guidorc 2a4d2ff
fix for schema normalizer
guidorc 48ed386
Merge branch 'master' into mcp-action-builder
guidorc 1af4045
simplify jsonSchema converter
guidorc c447320
Merge branch 'master' into mcp-action-builder
guidorc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
217 changes: 217 additions & 0 deletions
217
core/src/main/kotlin/org/evomaster/core/problem/mcp/builder/JsonSchemaToOpenApiConverter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| package org.evomaster.core.problem.mcp.builder | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode | ||
| import com.fasterxml.jackson.databind.ObjectMapper | ||
| import com.fasterxml.jackson.databind.node.ArrayNode | ||
| import com.fasterxml.jackson.databind.node.ObjectNode | ||
|
|
||
| /** | ||
| * Converts an MCP tool `inputSchema` (JSON Schema 2020-12) into OpenAPI 3.1 Schema | ||
| * Objects for the parser used by [org.evomaster.core.problem.rest.builder.RestActionBuilderV3]. | ||
| * | ||
| * The result is a flat map of schema-name -> node: the root schema is keyed by [rootName], and every | ||
| * `$defs` entry is lifted to a sibling entry so refs resolve across the set. | ||
| */ | ||
| object JsonSchemaToOpenApiConverter { | ||
|
|
||
| private val mapper = ObjectMapper() | ||
|
|
||
| private const val DEFS = "\$defs" | ||
|
|
||
| /** | ||
| * Convert an [inputSchema] into a flat map of OpenAPI-3.1 Schema Objects. | ||
| * | ||
| * @param rootName component name under which the root schema is registered | ||
| * @param inputSchema the raw JSON Schema node (typically an MCP tool `inputSchema`) | ||
| * @param messages list for warnings about lossy constructs | ||
| * @return schemas keyed by component name; always contains [rootName] | ||
| */ | ||
| fun convert(rootName: String, inputSchema: JsonNode, messages: MutableList<String>): Map<String, JsonNode> { | ||
|
|
||
| val result = LinkedHashMap<String, JsonNode>() | ||
|
|
||
| // Work on a deep copy so the caller's node is never mutated. | ||
| var rootCopy = inputSchema.deepCopy<JsonNode>() | ||
|
|
||
| val typeNode = rootCopy.get("type") | ||
| val isObject = rootCopy is ObjectNode && (typeNode == null || typeNode.asText() == "object" || (typeNode.isArray && typeNode.any { it.asText() == "object" })) | ||
|
|
||
| if (!isObject) { | ||
| messages.add("Schema at $rootName expected to be an object; replaced with empty object") | ||
| val newRoot = mapper.createObjectNode() | ||
| newRoot.put("type", "object") | ||
| rootCopy = newRoot | ||
| } else if (!rootCopy.has("type")) { | ||
| rootCopy.put("type", "object") | ||
| } | ||
|
|
||
| // Collect def names first so ref rewriting is aware of every sibling. | ||
| val defNames = HashSet<String>() | ||
| collectDefNames(rootCopy, defNames) | ||
|
|
||
| // Lift defs into siblings, disambiguating against the root name. | ||
| val defRename = HashMap<String, String>() | ||
| for (name in defNames) { | ||
| defRename[name] = if (name == rootName) "${name}_def" else name | ||
| } | ||
|
|
||
| // Extract and normalize defs first: normalizeNode strips `$defs` as it walks the tree, | ||
| // so they must be pulled out beforehand. | ||
| extractAndNormalizeDefs(rootCopy, defRename, result, messages) | ||
|
|
||
| val root = normalizeNode(rootCopy, rootName, defRename, messages) | ||
| result[rootName] = root | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| // ------------------------------------------------------------------------- | ||
|
|
||
| private fun collectDefNames(node: JsonNode, sink: MutableSet<String>) { | ||
| if (node !is ObjectNode) { | ||
| if (node is ArrayNode) node.forEach { collectDefNames(it, sink) } | ||
| return | ||
| } | ||
| val defs = node.get(DEFS) | ||
| if (defs is ObjectNode) { | ||
| defs.fieldNames().forEach { sink.add(it) } | ||
| } | ||
| node.fields().forEach { collectDefNames(it.value, sink) } | ||
| } | ||
|
|
||
| private fun extractAndNormalizeDefs( | ||
| node: JsonNode, | ||
| defRename: Map<String, String>, | ||
| result: MutableMap<String, JsonNode>, | ||
| messages: MutableList<String> | ||
| ) { | ||
| if (node !is ObjectNode) { | ||
| if (node is ArrayNode) node.forEach { extractAndNormalizeDefs(it, defRename, result, messages) } | ||
| return | ||
| } | ||
| val defs = node.get(DEFS) | ||
| if (defs is ObjectNode) { | ||
| defs.fields().forEach { (name, schema) -> | ||
| val target = defRename[name] ?: name | ||
| val normalized = normalizeNode(schema.deepCopy(), target, defRename, messages) | ||
| result[target] = normalized | ||
| } | ||
| } | ||
| // Recurse into any remaining nested defs before they get stripped by normalizeNode. | ||
| node.fields().forEach { extractAndNormalizeDefs(it.value, defRename, result, messages) } | ||
| } | ||
|
|
||
| /** | ||
| * Recursively rewrite a single schema node in place and return it. | ||
| */ | ||
| private fun normalizeNode( | ||
| node: JsonNode, | ||
| path: String, | ||
| defRename: Map<String, String>, | ||
| messages: MutableList<String> | ||
| ): JsonNode { | ||
|
|
||
| if (node is ArrayNode) { | ||
| for (i in 0 until node.size()) { | ||
| node.set(i, normalizeNode(node.get(i), path, defRename, messages)) | ||
| } | ||
| return node | ||
| } | ||
|
|
||
| if (node !is ObjectNode) { | ||
| return node | ||
| } | ||
|
|
||
| rewriteRef(node, defRename, messages, path) | ||
| rewriteConst(node) | ||
| rewritePrefixItems(node, messages, path) | ||
| stripDefs(node) | ||
|
|
||
| // Recurse into structural children. | ||
| recurseChild(node, "items", path, defRename, messages) | ||
| recurseChild(node, "additionalProperties", path, defRename, messages) | ||
| recurseChildrenOfObject(node, "properties", path, defRename, messages) | ||
| for (combiner in listOf("allOf", "anyOf", "oneOf")) { | ||
| recurseChild(node, combiner, path, defRename, messages) | ||
| } | ||
| recurseChild(node, "not", path, defRename, messages) | ||
|
|
||
| return node | ||
| } | ||
|
|
||
| private fun recurseChild( | ||
| node: ObjectNode, | ||
| field: String, | ||
| path: String, | ||
| defRename: Map<String, String>, | ||
| messages: MutableList<String> | ||
| ) { | ||
| val child = node.get(field) ?: return | ||
| // additionalProperties may be a boolean; leave scalars/booleans untouched. | ||
| if (child.isObject || child.isArray) { | ||
| node.set<JsonNode>(field, normalizeNode(child, "$path/$field", defRename, messages)) | ||
| } | ||
| } | ||
|
|
||
| private fun recurseChildrenOfObject( | ||
| node: ObjectNode, | ||
| field: String, | ||
| path: String, | ||
| defRename: Map<String, String>, | ||
| messages: MutableList<String> | ||
| ) { | ||
| val props = node.get(field) | ||
| if (props is ObjectNode) { | ||
| props.fields().forEach { (name, schema) -> | ||
| props.set<JsonNode>(name, normalizeNode(schema, "$path/$field/$name", defRename, messages)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // --- individual transformations ----------------------------------------- | ||
|
|
||
| /** `#/$defs/X` -> `#/components/schemas/X` (with def renaming). */ | ||
| private fun rewriteRef(node: ObjectNode, defRename: Map<String, String>, messages: MutableList<String>, path: String) { | ||
| val ref = node.get("\$ref")?.takeIf { it.isTextual }?.asText() ?: return | ||
| val local = when { | ||
| ref.startsWith("#/$DEFS/") -> ref.removePrefix("#/$DEFS/") | ||
| ref.startsWith("#/components/schemas/") -> ref.removePrefix("#/components/schemas/") | ||
| else -> { | ||
| messages.add("Unsupported \$ref '$ref' at $path; cannot resolve external/remote references") | ||
| return | ||
| } | ||
| } | ||
| val target = defRename[local] ?: local | ||
| node.put("\$ref", "#/components/schemas/$target") | ||
| } | ||
|
|
||
| /** `const: v` -> `enum: [v]` (the gene builder reads `enum`, not `const`). */ | ||
| private fun rewriteConst(node: ObjectNode) { | ||
| val const = node.remove("const") ?: return | ||
| node.putArray("enum").add(const) | ||
| } | ||
|
|
||
| /** Tuple validation `prefixItems: [...]` -> collapse to a single `items` schema (lossy). */ | ||
| private fun rewritePrefixItems(node: ObjectNode, messages: MutableList<String>, path: String) { | ||
| val prefix = node.get("prefixItems") | ||
| if (prefix is ArrayNode && prefix.size() > 0) { | ||
| if (node.get("items") == null) { | ||
| if (prefix.size() == 1) { | ||
| node.set<JsonNode>("items", prefix.get(0)) | ||
| } else { | ||
| val oneOf = mapper.createObjectNode() | ||
| val arr = oneOf.putArray("oneOf") | ||
| prefix.forEach { arr.add(it) } | ||
| node.set<JsonNode>("items", oneOf) | ||
| } | ||
| } | ||
| node.remove("prefixItems") | ||
| messages.add("Schema at $path used tuple 'prefixItems'; collapsed to a single 'items' schema (positional typing lost)") | ||
| } | ||
| } | ||
|
|
||
| /** Remove `$defs` after they've been lifted to siblings. */ | ||
| private fun stripDefs(node: ObjectNode) { | ||
| node.remove(DEFS) | ||
| } | ||
| } | ||
71 changes: 71 additions & 0 deletions
71
core/src/main/kotlin/org/evomaster/core/problem/mcp/builder/McpActionBuilder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package org.evomaster.core.problem.mcp.builder | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode | ||
| import org.evomaster.core.problem.mcp.McpToolCallAction | ||
| import org.evomaster.core.problem.mcp.client.McpToolDefinition | ||
| import org.evomaster.core.problem.rest.builder.RestActionBuilderV3 | ||
| import org.evomaster.core.search.gene.ObjectGene | ||
| import org.slf4j.Logger | ||
| import org.slf4j.LoggerFactory | ||
|
|
||
| /** | ||
| * Builds [McpToolCallAction]s from the tool definitions discovered on an MCP server. | ||
| * | ||
| * Converts each tool's `inputSchema` (JSON Schema 2020-12 dialect) into an [ObjectGene] that | ||
| * represents the tool's arguments, and registers one action per tool in the action cluster. | ||
| * | ||
| */ | ||
| object McpActionBuilder { | ||
|
|
||
| private val log: Logger = LoggerFactory.getLogger(McpActionBuilder::class.java) | ||
|
|
||
| /** | ||
| * Build an action per tool and register it in [actionCluster], keyed by the action id. | ||
| * | ||
| * @return warning messages accumulated while building (unsupported schema constructs, skipped tools) | ||
| */ | ||
| fun addActionsFromToolList( | ||
| tools: List<McpToolDefinition>, | ||
| actionCluster: MutableMap<String, McpToolCallAction>, | ||
| options: RestActionBuilderV3.Options | ||
| ): List<String> { | ||
|
|
||
| val messages = mutableListOf<String>() | ||
|
|
||
| for (tool in tools) { | ||
| try { | ||
| val gene = buildInputGene(tool.name, tool.inputSchema, options, messages) | ||
| val action = McpToolCallAction(tool.name, gene) | ||
| actionCluster[action.id] = action | ||
| } catch (e: Exception) { | ||
| messages.add("Skipping MCP tool '${tool.name}': ${e.message}") | ||
| log.warn("Failed to build action for MCP tool '{}'", tool.name, e) | ||
| } | ||
| } | ||
|
|
||
| return messages | ||
| } | ||
|
|
||
| /** | ||
| * Convert a single tool's `inputSchema` into an [ObjectGene]. | ||
| * | ||
| * @param toolName the MCP tool name, used as the root schema/component name | ||
| * @param inputSchema the raw JSON Schema node from the tool definition | ||
| * @param messages sink for warnings about lossy or unsupported schema constructs | ||
| */ | ||
| fun buildInputGene( | ||
| toolName: String, | ||
| inputSchema: JsonNode, | ||
| options: RestActionBuilderV3.Options, | ||
| messages: MutableList<String> | ||
| ): ObjectGene { | ||
|
|
||
| val schemas = JsonSchemaToOpenApiConverter.convert(toolName, inputSchema, messages) | ||
| val gene = RestActionBuilderV3.createGeneForDTOs(toolName, schemas, options) | ||
|
|
||
| return gene as? ObjectGene | ||
| ?: ObjectGene("input", listOf()).also { | ||
| messages.add("MCP tool '$toolName' inputSchema did not resolve to an object gene") | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extract into a function