Class McpToolUtils
java.lang.Object
io.meeds.mcp.server.util.McpToolUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringEvent parameter: whether that account is a person's or an agent's.static final StringEvent parameter: the login of the account that executed the call.static final StringEvent parameter: the agent chain, a JSON array of agent name ids.static final StringEvent parameter: the agent the call is made for, when known.static final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final StringEvent parameter: the argument limit kind the card may offer.static final StringEvent parameter: the argument limit value the card may offer.static final StringEvent parameter: when the created standing approval expires (epoch ms).static final StringEvent parameter: the standing approval a call ran under, or created.static final StringEvent parameter: the longest validity, in days, the card may offer.static final StringEvent parameter: the owner type of the standing approval.static final StringEvent parameter: whether the approval card may offer "Always allow".static final Stringstatic final Stringstatic final Stringstatic final StringEvent parameter: the person the call is for, absent for nobody.static final StringEvent parameter: what triggered the call.static final Stringstatic final Stringstatic final StringParameter of an approval request carrying the chat turn id of the call, so that the page can match the card to the call it started.static final Stringstatic final Stringstatic final StringWhat a published call input becomes when it can't be read as a JSON object: it is never published as is, since its content can't be checked.static final StringWhat a published call input becomes when it is too large to be shown.static final StringBound name of the argument that carries a base64 file, as the document tools name it.static final StringSuffix of the bound name (toCamelCase(String)of the argument name) of an argument that carries a base64 image:image_base64,imageBase64andimage_Base64all bind toimageBase64.static final Stringstatic final Stringstatic final intLongest call input replaced byARGUMENTS_NOT_READABLE_NOTEwhen it can't be read as a JSON object; a longer one is replaced byARGUMENTS_TOO_LARGE_NOTE.static final intLongest turn id echoed back to the chat; a longer one is dropped.static final Stringstatic final StringName of theExoFeatureServicefeature carrying the MCP server's global on/off flag.static final StringHeader of the internal client: the agent an agent account belongs to.static final StringHeader of the internal client: thePrincipalKindof the actor.static final StringHeader of the internal client naming the login of the account that executes the call; it and the other acting-identity headers are trusted under the same gate asTOOL_CONTEXT_USER_NAME_PARAM.static final StringHeader of the internal client: the agent chain, a JSON array of agent name ids (toAgentChainJson(List)).static final StringHeader naming the agent on whose behalf the internal client calls a tool, trusted under the same gate asTOOL_CONTEXT_USER_NAME_PARAM.static final Stringstatic final Stringstatic final Stringstatic final StringHeader of the internal client: the person the call is for, if any.static final StringHeader of the internal client: theOriginof the call.static final StringHeader carried by the internal client when the call belongs to a retried answer, trusted under the same gate asTOOL_CONTEXT_USER_NAME_PARAM.static final StringHeader of the internal client: the id of the only space the call may reach (ActingIdentity.spaceScope()), absent when the call isn't limited.static final StringRequest header of the internal MCP client carrying the chat turn that started the call.static final Stringstatic final Stringstatic final Stringstatic final String -
Method Summary
Modifier and TypeMethodDescriptionstatic StringformatDate(Long time) static StringformatDate(Date date) static final ToolDefinitionMethodsfromJsonString(String value) static ToolDefinitionMethodsfromJsonStringBase64(String value) Same asfromJsonString(String)for the persisted form, whose input schemas are base64 encoded.static StringgetClientId(org.springframework.security.core.Authentication authentication) Reads the OAuth client that owns the bearer token of a call.static ActingIdentityResolves who executes the current Tool call, for whom, through which agents and on which trigger.static StringResolves the agent on whose behalf the current Tool is executed, from theTOOL_CONTEXT_AGENT_NAME_ID_PARAMheader of the internal client only (getInternalToolCallRequest()): any other caller gets null, so an external client can never claim the agent a standing approval names.static StringResolves the AI chat conversation in which the current Tool is executed.static StringResolves the chat turn that started the current Tool call, from theTOOL_CONTEXT_TURN_ID_PARAMheader of the internal client only (getInternalToolCallRequest()).static StringResolves the user on behalf of whom the current Tool is executed: the kernelConversationStateidentity when set, else, for the internal client-credentials call only, theTOOL_CONTEXT_USER_NAME_PARAMrequest header, else the authenticated principal.static ObjectgetMethodToolFieldValue(org.springframework.ai.tool.method.MethodToolCallback toolCallback, String fieldName) Reads a private field of a Spring AIMethodToolCallback, which exposes no accessor for the tool object and method it wraps.static TimeZoneReads the acting user's time zone from their platform profile, so that a date a tool returns reads the same as the one the web UI shows them.static booleanTells whether the current Tool call belongs to a retried answer, from theTOOL_CONTEXT_RETRY_MESSAGE_ID_PARAMheader of the internal client only.static booleanisInternalClientAuthentication(org.springframework.security.core.Authentication authentication) Tells whether the authentication is the internal client-credentials call: a bearer token whose introspection attributes nameMCP_OAUTH2_CLIENT_CREDENTIALS_REGISTRATION_IDas the owning OAuth client (OAuth2TokenIntrospectionClaimNames.CLIENT_ID, set by the authorization server from the registered client the token was issued to).static booleanisInternalClientPrincipal(Map<String, Object> tokenAttributes) Same question asisInternalClientAuthentication(Authentication), asked one step earlier: at token introspection time there is noAuthenticationyet, only the introspected principal.static StringmarkdownToHtml(String markdown) Renders Markdown a tool produced into the HTML the platform's content fields expect, and leaves it untouched when it already carries HTML — a tool's output is not always Markdown, and rendering HTML twice mangles it.static StringnormalizeTurnId(String turnId) Bounds a client-supplied turn id.parseAgentChain(String value) Parses an agent chain serialized bytoAgentChainJson(List).static List<SimpleToolDefinition> parseToolDefinitions(URL url) Parses oneai-tool-definitions.jsonresource.static List<SimpleToolDefinition> parseToolDefinitions(URL url, Collection<String> leftOutToolNames) Same asparseToolDefinitions(URL), and adds the name of each tool left out for a key this server does not know to the given collection.static StringredactBase64ForPublication(String toolInput) Prepares a call input for publication to the chat, to the platform listeners and to the logs: an argument is matched on the name the tool binds it to,toCamelCase(String)of its name, so that every spelling a tool accepts is caught.static StringtoAgentChainJson(List<String> chain) Serializes an agent chain the way the acting-identity header and the execution events carry it: a JSON array, since an agent name id may hold a comma.static StringtoCamelCase(String name) static Datestatic StringtoJsonStringBase64(ToolDefinitionMethods toolDefinitions) Serializes tool definitions to their persisted form, base64 encoding each input schema.static StringtoSnakeCase(String name)
-
Field Details
-
AI_AGENT_TOOL_EXECUTION_EVENT
- See Also:
-
MCP_OAUTH2_CLIENT_CREDENTIALS_REGISTRATION_ID
- See Also:
-
MCP_SERVER_FEATURE
Name of theExoFeatureServicefeature carrying the MCP server's global on/off flag. The per-user audience is not a feature plugin: it is resolved byMcpServerAudienceServicealone.- See Also:
-
EVENT_MCP_SERVER_AUDIENCE_UPDATED
- See Also:
-
TOOL_CONTEXT_USER_NAME_PARAM
- See Also:
-
TOOL_CONTEXT_ID_PARAM
- See Also:
-
TOOL_CONTEXT_CONVERSATION_ID_PARAM
- See Also:
-
TOOL_CONTEXT_TURN_ID_PARAM
Request header of the internal MCP client carrying the chat turn that started the call. It is client-controlled and only an opaque correlation value: seegetCurrentTurnId().- See Also:
-
MAX_TURN_ID_LENGTH
public static final int MAX_TURN_ID_LENGTHLongest turn id echoed back to the chat; a longer one is dropped.- See Also:
-
BASE64_IMAGE_BOUND_NAME_SUFFIX
Suffix of the bound name (toCamelCase(String)of the argument name) of an argument that carries a base64 image:image_base64,imageBase64andimage_Base64all bind toimageBase64.- See Also:
-
BASE64_FILE_BOUND_NAME
Bound name of the argument that carries a base64 file, as the document tools name it.- See Also:
-
MAX_PUBLISHED_INPUT_LENGTH
public static final int MAX_PUBLISHED_INPUT_LENGTHLongest call input replaced byARGUMENTS_NOT_READABLE_NOTEwhen it can't be read as a JSON object; a longer one is replaced byARGUMENTS_TOO_LARGE_NOTE.- See Also:
-
ARGUMENTS_TOO_LARGE_NOTE
What a published call input becomes when it is too large to be shown.- See Also:
-
ARGUMENTS_NOT_READABLE_NOTE
What a published call input becomes when it can't be read as a JSON object: it is never published as is, since its content can't be checked.- See Also:
-
TOOL_CONTEXT_AGENT_NAME_ID_PARAM
Header naming the agent on whose behalf the internal client calls a tool, trusted under the same gate asTOOL_CONTEXT_USER_NAME_PARAM.- See Also:
-
TOOL_CONTEXT_RETRY_MESSAGE_ID_PARAM
Header carried by the internal client when the call belongs to a retried answer, trusted under the same gate asTOOL_CONTEXT_USER_NAME_PARAM.- See Also:
-
TOOL_CONTEXT_ACTOR_USER_NAME_PARAM
Header of the internal client naming the login of the account that executes the call; it and the other acting-identity headers are trusted under the same gate asTOOL_CONTEXT_USER_NAME_PARAM.- See Also:
-
TOOL_CONTEXT_ACTOR_KIND_PARAM
Header of the internal client: thePrincipalKindof the actor.- See Also:
-
TOOL_CONTEXT_ACTOR_AGENT_NAME_ID_PARAM
Header of the internal client: the agent an agent account belongs to.- See Also:
-
TOOL_CONTEXT_ON_BEHALF_OF_PARAM
Header of the internal client: the person the call is for, if any.- See Also:
-
TOOL_CONTEXT_AGENT_CHAIN_PARAM
Header of the internal client: the agent chain, a JSON array of agent name ids (toAgentChainJson(List)).- See Also:
-
TOOL_CONTEXT_ORIGIN_PARAM
-
TOOL_CONTEXT_SPACE_SCOPE_PARAM
Header of the internal client: the id of the only space the call may reach (ActingIdentity.spaceScope()), absent when the call isn't limited. Read with the other acting-identity headers only, and refused on a call that sends the legacy headers alone.- See Also:
-
TOOL_CONTEXT_ID
-
TOOL_READ_SCOPE
- See Also:
-
TOOL_WRITE_SCOPE
- See Also:
-
TOOL_WRITE_APPROVE_SCOPE
- See Also:
-
EVENT_TOOL_UPDATED
- See Also:
-
AI_AGENT_TOOL_APPROVED_PARAM
- See Also:
-
AI_AGENT_TOOL_USERNAME_PARAM
- See Also:
-
AI_AGENT_TOOL_CONVERSATION_ID_PARAM
- See Also:
-
AI_AGENT_TOOL_TURN_ID_PARAM
Parameter of an approval request carrying the chat turn id of the call, so that the page can match the card to the call it started.- See Also:
-
AI_AGENT_TOOL_INPUT_PARAM
- See Also:
-
AI_AGENT_TOOL_OUTPUT_PARAM
- See Also:
-
AI_AGENT_TOOL_TYPE_PARAM
- See Also:
-
AI_AGENT_TOOL_DURATION_PARAM
- See Also:
-
AI_AGENT_TOOL_START_TIME_PARAM
- See Also:
-
AI_AGENT_TOOL_ID_PARAM
- See Also:
-
AI_AGENT_TOOL_NAME_PARAM
- See Also:
-
AI_AGENT_TOOL_EXEC_COMPLETED_PARAM
- See Also:
-
AI_AGENT_TOOL_GRANT_ID_PARAM
Event parameter: the standing approval a call ran under, or created.- See Also:
-
AI_AGENT_TOOL_GRANT_OWNER_TYPE_PARAM
Event parameter: the owner type of the standing approval.- See Also:
-
AI_AGENT_TOOL_GRANT_EXPIRES_AT_PARAM
Event parameter: when the created standing approval expires (epoch ms).- See Also:
-
AI_AGENT_TOOL_GRANTABLE_PARAM
Event parameter: whether the approval card may offer "Always allow".- See Also:
-
AI_AGENT_TOOL_GRANT_MAX_DAYS_PARAM
Event parameter: the longest validity, in days, the card may offer.- See Also:
-
AI_AGENT_TOOL_AGENT_NAME_ID_PARAM
Event parameter: the agent the call is made for, when known.- See Also:
-
AI_AGENT_TOOL_ACTOR_USER_NAME_PARAM
Event parameter: the login of the account that executed the call.- See Also:
-
AI_AGENT_TOOL_ACTOR_KIND_PARAM
Event parameter: whether that account is a person's or an agent's.- See Also:
-
AI_AGENT_TOOL_ON_BEHALF_OF_PARAM
Event parameter: the person the call is for, absent for nobody.- See Also:
-
AI_AGENT_TOOL_AGENT_CHAIN_PARAM
Event parameter: the agent chain, a JSON array of agent name ids.- See Also:
-
AI_AGENT_TOOL_ORIGIN_PARAM
Event parameter: what triggered the call.- See Also:
-
AI_AGENT_TOOL_GRANT_CONSTRAINT_KIND_PARAM
Event parameter: the argument limit kind the card may offer.- See Also:
-
AI_AGENT_TOOL_GRANT_CONSTRAINT_VALUE_PARAM
Event parameter: the argument limit value the card may offer.- See Also:
-
-
Method Details
-
parseToolDefinitions
Parses oneai-tool-definitions.jsonresource. A tool that declares no icon takes the file's default icon, when the file declares one. A key the file declares at its root and this server does not know is ignored. A tool entry carrying a key this server does not know is left out, with a WARN, and the file's other tools load: loaded, the tool would run with the default of whatever field the key misspells, a misspelledrequire_approvalorannotationskey included. The keys insideannotationsare read by the MCP SDK'sToolAnnotations, which ignores the ones it does not know.- Parameters:
url- the resource to read- Returns:
- the tool definitions it declares, empty when it cannot be read
-
parseToolDefinitions
public static List<SimpleToolDefinition> parseToolDefinitions(URL url, Collection<String> leftOutToolNames) Same asparseToolDefinitions(URL), and adds the name of each tool left out for a key this server does not know to the given collection.- Parameters:
url- the resource to readleftOutToolNames- receives the names of the tools left out- Returns:
- the tool definitions it declares, empty when it cannot be read
-
fromJsonString
- Parameters:
value- the tool definitions as a JSON string- Returns:
- the parsed definitions, or null when the string is blank
-
fromJsonStringBase64
Same asfromJsonString(String)for the persisted form, whose input schemas are base64 encoded. A saved tool carrying a key this server does not know is kept, with a WARN: the persisted form is written bytoJsonStringBase64(ToolDefinitionMethods)alone, so such a key comes from a newer server, not from a misspelling, and dropping the tool would replace the administrator'srequire_approvalanddisabledwith the shipped values.- Parameters:
value- the persisted tool definitions- Returns:
- the parsed definitions with decoded schemas, or null when blank
-
toJsonStringBase64
Serializes tool definitions to their persisted form, base64 encoding each input schema.- Parameters:
toolDefinitions- the definitions to serialize- Returns:
- the JSON string to persist
-
toSnakeCase
-
toCamelCase
-
getMethodToolFieldValue
public static Object getMethodToolFieldValue(org.springframework.ai.tool.method.MethodToolCallback toolCallback, String fieldName) throws NoSuchFieldException, IllegalAccessException Reads a private field of a Spring AIMethodToolCallback, which exposes no accessor for the tool object and method it wraps.- Parameters:
toolCallback- the callback to introspectfieldName- the field to read- Returns:
- the field value
- Throws:
NoSuchFieldException- when the field does not existIllegalAccessException- when it cannot be read
-
getCurrentUserName
Resolves the user on behalf of whom the current Tool is executed: the kernelConversationStateidentity when set, else, for the internal client-credentials call only, theTOOL_CONTEXT_USER_NAME_PARAMrequest header, else the authenticated principal. The header is trusted under the gate ofgetInternalToolCallRequest()and nowhere else; the internal call is recognized by the OAuth client that owns the token (isInternalClientAuthentication(Authentication)), never by the token subject, so a user whose login happens to be the internal client id is just that user.- Returns:
- the user name, or null when no user can be resolved
-
getCurrentActingIdentity
Resolves who executes the current Tool call, for whom, through which agents and on which trigger. For the internal client-credentials call only (the gate ofgetInternalToolCallRequest()), the identity is read from its headers: the acting-identity headers when it sends them, else the legacyTOOL_CONTEXT_USER_NAME_PARAMandTOOL_CONTEXT_AGENT_NAME_ID_PARAMones, the person in a chat with the named agent appended, or with the named source scope (a value holding ":", never an agent hop). For any other caller the identity is the token's user acting for themself through an external client, whatever headers it sends. The identity is resolved once per request and kept as a request attribute, so a tool reading it during the call sees the one the server checked. The space scope (TOOL_CONTEXT_SPACE_SCOPE_PARAM) is part of the identity the internal client sends, so an external client can neither set nor lift it.- Returns:
- the acting identity, or null when no user can be resolved
- Throws:
IllegalStateException- when the internal client sends an identity the server refuses: an incomplete or malformed one, an agent account acting for a person, a space scope that isn't a space id, or a space scope with the legacy headers alone
-
toAgentChainJson
Serializes an agent chain the way the acting-identity header and the execution events carry it: a JSON array, since an agent name id may hold a comma.- Parameters:
chain- the agent name ids, may be null- Returns:
- the JSON array,
[]for a null chain
-
parseAgentChain
Parses an agent chain serialized bytoAgentChainJson(List).- Parameters:
value- the JSON array- Returns:
- the agent name ids, in order
- Throws:
IllegalArgumentException- when the value isn't a JSON array of agent name ids: blank, not an array, an element that isn't a string, or one that is blank or holds ":"
-
getCurrentConversationId
Resolves the AI chat conversation in which the current Tool is executed. The id travels in theTOOL_CONTEXT_CONVERSATION_ID_PARAMrequest header set by the internal MCP client and is trusted under exactly the same gate as the user name (getInternalToolCallRequest()): any other caller, an external OAuth MCP client for instance, gets null whatever it sends, so it can never choose the conversation its Tool executions are recorded in.- Returns:
- the conversation id, or null when the call isn't the internal client's or carries no conversation
-
getCurrentTurnId
Resolves the chat turn that started the current Tool call, from theTOOL_CONTEXT_TURN_ID_PARAMheader of the internal client only (getInternalToolCallRequest()). The value is client-controlled: it is only echoed back, as an opaque correlation value, to the channel of the requesting user and on theai-agent-tool-executionevent, and never decides a grant, an approval or an identity.- Returns:
- the turn id, or null when the call isn't the internal client's,
carries none or one longer than
MAX_TURN_ID_LENGTH
-
normalizeTurnId
Bounds a client-supplied turn id.- Parameters:
turnId- the raw value, may be null- Returns:
- the trimmed value, or null when blank or longer than
MAX_TURN_ID_LENGTH
-
redactBase64ForPublication
Prepares a call input for publication to the chat, to the platform listeners and to the logs: an argument is matched on the name the tool binds it to,toCamelCase(String)of its name, so that every spelling a tool accepts is caught. Each top-level string argument whose bound name ends with "Base64" is replaced by[image, <size>], and the one whose bound name is exactly "base64" by[file, <size>], the size being the decoded one (KB below 1 MB, otherwise MB with one decimal). Every input is parsed, whatever its length, with the mapper the tool's arguments are bound with (Spring AI's default mapper), so an input the tool accepts is read the same way here, and argument names are read once parsed: a name written with a JSON escape, in another case or with underscores the binding drops is found too. An input without such an argument is returned as is, never re-serialised, when it is exactly one JSON object; one the tool reads with something after the object is returned as the object it reads, re-serialised, so the rest is never published. The publication fails closed: an input that can't be read as a JSON object is never returned as is, but replaced byARGUMENTS_NOT_READABLE_NOTE, or byARGUMENTS_TOO_LARGE_NOTEwhen longer than 65536 characters. It is meant for what is published only: the input a tool runs on, or a grant is matched on, must not go through it.- Parameters:
toolInput- the call input, may be null- Returns:
- the input to publish
-
getCurrentAgentNameId
Resolves the agent on whose behalf the current Tool is executed, from theTOOL_CONTEXT_AGENT_NAME_ID_PARAMheader of the internal client only (getInternalToolCallRequest()): any other caller gets null, so an external client can never claim the agent a standing approval names.- Returns:
- the agent name id, or null when unknown
-
isCurrentCallRetry
public static boolean isCurrentCallRetry()Tells whether the current Tool call belongs to a retried answer, from theTOOL_CONTEXT_RETRY_MESSAGE_ID_PARAMheader of the internal client only. The answer can only make the approval gate stricter (no standing approval applies to a retried answer), so no caller gains anything by sending it, and an external client's header is ignored like the others.- Returns:
- true when the internal client marked the call as a retry
-
getClientId
Reads the OAuth client that owns the bearer token of a call.- Parameters:
authentication- the currentAuthentication, may be null- Returns:
- the client id, or null when the call isn't a bearer-token call
-
isInternalClientAuthentication
public static boolean isInternalClientAuthentication(org.springframework.security.core.Authentication authentication) Tells whether the authentication is the internal client-credentials call: a bearer token whose introspection attributes nameMCP_OAUTH2_CLIENT_CREDENTIALS_REGISTRATION_IDas the owning OAuth client (OAuth2TokenIntrospectionClaimNames.CLIENT_ID, set by the authorization server from the registered client the token was issued to). The token subject is deliberately not consulted: it is the client id for a client-credentials grant but the user login for a user grant, so a user whose login is the internal client id would otherwise enter the internal path with an ordinary user token.- Parameters:
authentication- the currentAuthentication, may be null- Returns:
- true when the token belongs to the internal client, else false
-
isInternalClientPrincipal
Same question asisInternalClientAuthentication(Authentication), asked one step earlier: at token introspection time there is noAuthenticationyet, only the introspected principal. Both enforcement points of the MCP access gate must recognize the internal client the same way or the exemption means two different things, so both go throughisInternalClientId(Object)and neither consults the token subject.- Parameters:
tokenAttributes- the introspected token attributes, may be null- Returns:
- true when the token belongs to the internal client, else false
-
markdownToHtml
Renders Markdown a tool produced into the HTML the platform's content fields expect, and leaves it untouched when it already carries HTML — a tool's output is not always Markdown, and rendering HTML twice mangles it.- Parameters:
markdown- the tool output, may be null or blank- Returns:
- the rendered HTML, or the input unchanged when it is blank, when it already looks like HTML, or when rendering failed
-
toDate
-
formatDate
-
formatDate
-
getUserTimeZone
Reads the acting user's time zone from their platform profile, so that a date a tool returns reads the same as the one the web UI shows them.- Returns:
- the user's time zone, or null when the profile carries none — the caller then falls back to the server's default
-