Merged
fix(mcp): add structured data for tool input validation errors #367
zhuyutong888Add on 19 days ago
fix(mcp): add structured data for tool input validation errors #367
Merged
Total: 7 files changed+308-11
| @@ -107,7 +107,8 @@ void McpServerImplement::ReceiveIncomingMessages(const RequestId& requestId, con | |||
| 107 | void McpServerImplement::SendErrorResponse(const RequestId& requestId, | 107 | void McpServerImplement::SendErrorResponse(const RequestId& requestId, |
| 108 | JsonRpcErrorCode code, | 108 | JsonRpcErrorCode code, |
| 109 | const std::string& message, | 109 | const std::string& message, |
| 110 | - ServerRequestContext& ctx) | 110 | + ServerRequestContext& ctx, |
| 111 | + const std::optional<JsonValue>& data) | ||
| 111 | { | 112 | { |
| 112 | auto session = ctx.activeSession.lock(); | 113 | auto session = ctx.activeSession.lock(); |
| 113 | if (session == nullptr) { | 114 | if (session == nullptr) { |
| @@ -119,6 +120,7 @@ void McpServerImplement::SendErrorResponse(const RequestId& requestId, | |||
| 119 | error.id_ = requestId; | 120 | error.id_ = requestId; |
| 120 | error.code_ = static_cast<int>(code); | 121 | error.code_ = static_cast<int>(code); |
| 121 | error.message_ = message; | 122 | error.message_ = message; |
| 123 | + error.data_ = data; | ||
| 122 | session->SendResponse(requestId, error, ctx); | 124 | session->SendResponse(requestId, error, ctx); |
| 123 | } | 125 | } |
| 124 | 126 | ||
| @@ -168,6 +170,9 @@ void McpServerImplement::HandleToolsCall(const RequestId& requestId, const Reque | |||
| 168 | session->SendResponse(requestId, result, ctx); | 170 | session->SendResponse(requestId, result, ctx); |
| 169 | } | 171 | } |
| 170 | // Asynchronous function has no return value, response will be sent via callback | 172 | // Asynchronous function has no return value, response will be sent via callback |
| 173 | + } catch (const ToolInputValidationError& e) { | ||
| 174 | + MCP_LOG(MCP_LOG_LEVEL_INFO, "CallTool: caught exception: " + std::string(e.what())); | ||
| 175 | + SendErrorResponse(requestId, JsonRpcErrorCode::INVALID_PARAMS, e.what(), ctx, e.Data()); | ||
| 171 | } catch (const std::exception& e) { | 176 | } catch (const std::exception& e) { |
| 172 | MCP_LOG(MCP_LOG_LEVEL_INFO, "CallTool: caught exception: " + std::string(e.what())); | 177 | MCP_LOG(MCP_LOG_LEVEL_INFO, "CallTool: caught exception: " + std::string(e.what())); |
| 173 | SendErrorResponse(requestId, JsonRpcErrorCode::INVALID_PARAMS, e.what(), ctx); | 178 | SendErrorResponse(requestId, JsonRpcErrorCode::INVALID_PARAMS, e.what(), ctx); |
| @@ -10,6 +10,7 @@ | |||
| 10 | #include <istream> | 10 | #include <istream> |
| 11 | #include <memory> | 11 | #include <memory> |
| 12 | #include <mutex> | 12 | #include <mutex> |
| 13 | +#include <optional> | ||
| 13 | #include <string> | 14 | #include <string> |
| 14 | #include <unordered_map> | 15 | #include <unordered_map> |
| 15 | #include <vector> | 16 | #include <vector> |
| @@ -89,7 +90,7 @@ private: | |||
| 89 | void HandleComplete(const RequestId& requestId, const Request& request, ServerRequestContext& ctx); | 90 | void HandleComplete(const RequestId& requestId, const Request& request, ServerRequestContext& ctx); |
| 90 | 91 | ||
| 91 | void SendErrorResponse(const RequestId& requestId, JsonRpcErrorCode code, const std::string& message, | 92 | void SendErrorResponse(const RequestId& requestId, JsonRpcErrorCode code, const std::string& message, |
| 92 | - ServerRequestContext& ctx); | 93 | + ServerRequestContext& ctx, const std::optional<JsonValue>& data = std::nullopt); |
| 93 | 94 | ||
| 94 | bool ValidateConfig(const ServerConfig& config); | 95 | bool ValidateConfig(const ServerConfig& config); |
| 95 | bool ValidateStreamableHttpConfig(const StreamableHttpServerConfig& config); | 96 | bool ValidateStreamableHttpConfig(const StreamableHttpServerConfig& config); |
MMCP/cpp-sdk/tests/ut/client/client_test.cpp+32-0File content is under review, please try again later
MMCP/cpp-sdk/tests/ut/server/tool_manager_test.cpp+180-1File content is under review, please try again later
| @@ -136,7 +136,7 @@ function(_mcp_fetch_git_package) | |||
| 136 | endfunction() | 136 | endfunction() |
| 137 | 137 | ||
| 138 | function(fetch_or_find_package) | 138 | function(fetch_or_find_package) |
| 139 | - set(oneValueArgs NAME GIT_REPO GIT_TAG SYSTEM_PACKAGE_NAME TARGET_NAME) | 139 | + set(oneValueArgs NAME GIT_REPO GIT_TAG SYSTEM_PACKAGE_NAME TARGET_NAME MIN_VERSION) |
| 140 | set(multiValueArgs OPTIONS) | 140 | set(multiValueArgs OPTIONS) |
| 141 | cmake_parse_arguments(ARG "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) | 141 | cmake_parse_arguments(ARG "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) |
| 142 | 142 | ||
| @@ -158,8 +158,22 @@ function(fetch_or_find_package) | |||
| 158 | if(${OPTION_VAR}) | 158 | if(${OPTION_VAR}) |
| 159 | find_package(${ARG_SYSTEM_PACKAGE_NAME} QUIET) | 159 | find_package(${ARG_SYSTEM_PACKAGE_NAME} QUIET) |
| 160 | if(${ARG_SYSTEM_PACKAGE_NAME}_FOUND) | 160 | if(${ARG_SYSTEM_PACKAGE_NAME}_FOUND) |
| 161 | - set(PACKAGE_FOUND TRUE) | 161 | + set(_mcp_system_version_ok TRUE) |
| 162 | - message(STATUS "Using system ${ARG_NAME} (find_package)") | 162 | + if(ARG_MIN_VERSION) |
| 163 | + if(DEFINED ${ARG_SYSTEM_PACKAGE_NAME}_VERSION AND NOT "${${ARG_SYSTEM_PACKAGE_NAME}_VERSION}" STREQUAL "") | ||
| 164 | + if(${ARG_SYSTEM_PACKAGE_NAME}_VERSION VERSION_LESS ARG_MIN_VERSION) | ||
| 165 | + set(_mcp_system_version_ok FALSE) | ||
| 166 | + message(STATUS "System ${ARG_NAME} ${${ARG_SYSTEM_PACKAGE_NAME}_VERSION} is older than required ${ARG_MIN_VERSION}; fetching") | ||
| 167 | + endif() | ||
| 168 | + else() | ||
| 169 | + set(_mcp_system_version_ok FALSE) | ||
| 170 | + message(STATUS "System ${ARG_NAME} version is unknown; fetching ${ARG_GIT_TAG} (requires >= ${ARG_MIN_VERSION})") | ||
| 171 | + endif() | ||
| 172 | + endif() | ||
| 173 | + if(_mcp_system_version_ok) | ||
| 174 | + set(PACKAGE_FOUND TRUE) | ||
| 175 | + message(STATUS "Using system ${ARG_NAME} (find_package)") | ||
| 176 | + endif() | ||
| 163 | endif() | 177 | endif() |
| 164 | 178 | ||
| 165 | if(NOT PACKAGE_FOUND) | 179 | if(NOT PACKAGE_FOUND) |
| @@ -171,7 +185,11 @@ function(fetch_or_find_package) | |||
| 171 | list(REMOVE_DUPLICATES _mcp_pkg_names) | 185 | list(REMOVE_DUPLICATES _mcp_pkg_names) |
| 172 | 186 | ||
| 173 | foreach(_mcp_pkg_name IN LISTS _mcp_pkg_names) | 187 | foreach(_mcp_pkg_name IN LISTS _mcp_pkg_names) |
| 174 | - pkg_check_modules(_mcp_pkg QUIET IMPORTED_TARGET "${_mcp_pkg_name}") | 188 | + set(_mcp_pkg_spec "${_mcp_pkg_name}") |
| 189 | + if(ARG_MIN_VERSION) | ||
| 190 | + set(_mcp_pkg_spec "${_mcp_pkg_name}>=${ARG_MIN_VERSION}") | ||
| 191 | + endif() | ||
| 192 | + pkg_check_modules(_mcp_pkg QUIET IMPORTED_TARGET "${_mcp_pkg_spec}") | ||
| 175 | if(_mcp_pkg_FOUND AND _mcp_pkg_INCLUDE_DIRS AND _mcp_pkg_LIBRARIES | 193 | if(_mcp_pkg_FOUND AND _mcp_pkg_INCLUDE_DIRS AND _mcp_pkg_LIBRARIES |
| 176 | AND NOT "${_mcp_pkg_INCLUDE_DIRS}" STREQUAL "NOTFOUND" | 194 | AND NOT "${_mcp_pkg_INCLUDE_DIRS}" STREQUAL "NOTFOUND" |
| 177 | AND NOT "${_mcp_pkg_LIBRARIES}" STREQUAL "NOTFOUND") | 195 | AND NOT "${_mcp_pkg_LIBRARIES}" STREQUAL "NOTFOUND") |
| @@ -240,10 +258,13 @@ fetch_or_find_package( | |||
| 240 | ) | 258 | ) |
| 241 | 259 | ||
| 242 | list(APPEND CMAKE_PREFIX_PATH "/usr/local") | 260 | list(APPEND CMAKE_PREFIX_PATH "/usr/local") |
| 261 | +# Requires >= 2.3.0: combination keyword probes use the validator's internal | ||
| 262 | +# first_error_handler, so caller-provided handlers only see final failures. | ||
| 243 | fetch_or_find_package( | 263 | fetch_or_find_package( |
| 244 | NAME nlohmann_json_schema_validator | 264 | NAME nlohmann_json_schema_validator |
| 245 | GIT_REPO https://github.com/pboettch/json-schema-validator.git | 265 | GIT_REPO https://github.com/pboettch/json-schema-validator.git |
| 246 | GIT_TAG 2.3.0 | 266 | GIT_TAG 2.3.0 |
| 267 | + MIN_VERSION 2.3.0 | ||
| 247 | SYSTEM_PACKAGE_NAME nlohmann_json_schema_validator | 268 | SYSTEM_PACKAGE_NAME nlohmann_json_schema_validator |
| 248 | TARGET_NAME nlohmann_json_schema_validator | 269 | TARGET_NAME nlohmann_json_schema_validator |
| 249 | ) | 270 | ) |