Repository navigation
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request introduces a common utility package mssqlcommon to handle the conversion of SQL Server specific column types (such as DECIMAL, MONEY, SMALLMONEY, and UNIQUEIDENTIFIER) from raw byte slices to JSON-friendly formats (strings and canonical GUIDs). This utility is integrated into both the standard MSSQL and Cloud SQL MSSQL sources. The feedback suggests explicitly adding NUMERIC to the type conversion switch case to ensure robust handling across different driver versions.
|
Friendly ping on this one, opened 18 October — I don't think anything here is waiting on me. On the review comment. The suggestion is to add On CI. This does change the JSON that existing users receive for the affected column types, which is |
… in severe errors (#4445) ## Description For SQL Server errors of severity 20 and above, the `mssql` and `cloud-sql-mssql` sources answer with only `SQL Server had internal error`: no error number, severity or message (#4444). go-mssqldb returns `mssql.ServerError` for these errors. Its `Error()` is that fixed text; the errors the server sent are kept in the `mssql.Error` it wraps. That error's own fields describe the *last* error, which is usually a consequence ("the session is in the kill state"); its `All` field lists every error from first to last, and the first is the cause. This PR adds `mssqlcommon.WithServerErrorDetails`, which `RunSQL` in both sources now applies at its two error returns (the query call and `results.Err()`). For an `mssql.ServerError` it appends every entry of `All`, in order, formatted from its fields, after the existing text, and keeps the original error wrapped, so `errors.As` still finds it. Every other error is returned unchanged. `RAISERROR('severe error text', 20, 1) WITH LOG` through `mssql-execute-sql`, before and after (SQL Server 2025 CU10): ```text before: error processing request: unable to execute query: SQL Server had internal error after: error processing request: unable to execute query: SQL Server had internal error: severe error text (error 50000, severity 20, state 1); Process ID 89 has raised user error 50000, severity 20. SQL Server is terminating this process. (error 2745, severity 16, state 2); Cannot continue the execution because the session is in the kill state. (error 596, severity 21, state 1) ``` Design notes: - The text is built from the fields, not from `mssql.Error.Error()`, whose format changes in go-mssqldb v1.11 (microsoft/go-mssqldb#250, which #4246 brings in). The output is the same on v1.10.0 and v1.11.2. - `error` is lower case on purpose: `util.ProcessGeneralError` classifies by `Error 401`, `Error 403` and `Error 429`, and SQL Server has errors with those numbers. A unit test checks that the category stays `AGENT_ERROR`. - The `SQL Server had internal error` prefix stays, for clients that match on it. - The helper lives in its own file in `internal/tools/mssql/mssqlcommon`, following `mysqlcommon`. #4284 adds `mssqlcommon.go` to the same package, so the two merge in either order. Testing: - Unit tests for the helper in `internal/tools/mssql/mssqlcommon/errors_test.go`. `mssql.ServerError` keeps the wrapped error in an unexported field, so the tests stand in for it with `errors.Join(mssql.Error{...}, mssql.ServerError{})`. - The Linux job of `tests.yaml` (build, both `go test -race` runs, coverage gate), the `go mod tidy` check, and golangci-lint v2.15.0 on the changed packages (0 issues), all in a `golang:1.27.3` container. - Coverage: the new `mssqlcommon` code is at 100%. `internal/sources/mssql` (8.2%) and `internal/sources/cloudsqlmssql` (7.9%) are the same as on `main`; `RunSQL`, where the two call sites are, is reached only by the integration tests. - Live, with Toolbox built from this branch with the repository's `Dockerfile`, against SQL Server 2025 CU10: both error returns (the query call above, and `results.Err()` via `SELECT 1 AS a; RAISERROR(...)`) now carry the chain. Severity 16 errors, divide by zero, result sets and multiple result sets are unchanged. The chain the helper formats is the same on SQL Server 2017 CU31, 2019 CU32, 2022 CU27 and 2025 CU10, with go-mssqldb v1.10.0 and v1.11.2 (details in #4444). - No integration test. Raising severity 19 and above `WITH LOG` needs a login in the sysadmin role or with `ALTER TRACE` (a plain login gets error 2778), and I don't know whether the CI logins have either. If they do, I can add one to `tests/mssql`. - `cloud-sql-mssql` gets the same change at the same two returns; I couldn't run it against Cloud SQL. Security: - No query is built or changed, and no user input reaches SQL. - The added text is SQL Server's own error messages, which Toolbox already returns for errors below severity 20. No credentials, connection strings or parameters are added. - No file paths, commands or auth checks are touched. ## PR Checklist - [x] Make sure you reviewed [CONTRIBUTING.md](https://github.com/googleapis/mcp-toolbox/blob/main/CONTRIBUTING.md) - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/mcp-toolbox/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure you have manually reviewed the entire diff before requesting a review - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary): no docs change needed - [ ] Make sure to add `!` if this involve a breaking change: not breaking; the existing text stays as the prefix 🛠️ Fixes #4444 --------- Co-authored-by: Anubhav Dhawan <[email protected]>
…D values as text instead of base64 go-mssqldb scans DECIMAL, NUMERIC, MONEY and SMALLMONEY values as their decimal text in a []byte, and UNIQUEIDENTIFIER values as their 16 wire bytes. RunSQL put these into the row unchanged, so encoding/json wrote them as base64: SubTotal 20565.6206 came back as "MjA1NjUuNjIwNg==". Convert by the column's DatabaseTypeName in a shared mssqlcommon package, as mysqlcommon does for MySQL: decimals become their exact decimal text (no float rounding), GUIDs their canonical string. Binary types stay base64.
…s well as DECIMAL go-mssqldb reports DECIMAL for NUMERIC columns, so this changes no behaviour with the current driver. Listing NUMERIC makes the set of converted type names explicit, and keeps the conversion correct if a driver version ever reports NUMERIC directly.
c9278e4 to
c9459c7
Compare
Description
RunSQLin themssqlandcloud-sql-mssqlsources scans every column intoanyand adds thevalue to the row unchanged. go-mssqldb returns DECIMAL, NUMERIC, MONEY and SMALLMONEY values as
their decimal text in a
[]byteand UNIQUEIDENTIFIER values as their 16 wire bytes, andencoding/jsonwrites a[]byteas base64. So these values reach the client base64-encoded andan agent can't read or compare them:
{"m":"NDQwMDU5Mi44MDA0","d":"MTIzNDU2Nzg5MDEyMzQ1Njc4OTAuMDEyMzQ1Njc4OQ==","g":"/xmWb4aLEdC0LQDAT8lk/w=="}Solution. A new
internal/tools/mssql/mssqlcommon.ConvertToType(databaseTypeName, value),the SQL Server counterpart of the existing
mysqlcommon.ConvertToType, keyed on the column'sColumnType.DatabaseTypeName(). Decimals become their exact decimal text, so there is no floatrounding at any precision; GUIDs become their canonical string via go-mssqldb's own
UniqueIdentifier. Every other value, including BINARY, VARBINARY and IMAGE, is returnedunchanged.
DatabaseTypeName()reportsDECIMALfor NUMERIC columns too, so one case coversboth. Both sources call it in
RunSQL(7 lines each);ColumnTypes()is fetched once per query,not per row. Same result:
{"m":"4400592.8004","d":"12345678901234567890.0123456789","g":"6F9619FF-8B86-D011-B42D-00C04FC964FF"}Impact — this is a visible change to the JSON existing users receive, for the affected column
types only. DECIMAL/NUMERIC/MONEY/SMALLMONEY change from the base64 of the decimal text to the
decimal text, and UNIQUEIDENTIFIER from the base64 of its wire bytes to the GUID string. Both
remain JSON strings, so no JSON type changes, and binary columns are untouched. Anyone currently
base64-decoding these fields to read them would stop needing to. This matches what Toolbox
already does for MySQL DECIMAL (#840), BigQuery NUMERIC (#2084) and Postgres UUIDs (#3806).
Verification. Built from source and called through MCP (
tools/callon/mcp, protocol2025-06-18) against the official SQL Server 2019 (15.0.4500.2), 2022 (16.0.4305.5) and 2025(17.0.5015.4) containers; output is identical on all three. Covered positive, negative, zero and
NULL decimals,
decimal(38,0)anddecimal(38,10)at full precision, MONEY and SMALLMONEY attheir minimum and maximum, a
numeric(5,1)column, and NULL GUIDs; the returned GUID matchesSQL Server's own
CAST(... AS char(36))text, so the byte order is right.int,bigint(exact, no float rounding),
float,datetime2,time,nvarchar,varbinaryandbitareunchanged.
go test ./cmd/... ./internal/...passes (426 packages, 0 failures), as does-race;go vet,gofmtandgolangci-lint runare clean. Applying only this PR's test fileson top of unmodified
mainmakes the new integration test fail with exactly the base64 values,so it does catch the regression.
On a 1000-row result with one decimal and one GUID per row, median latency over 20 warm calls
goes from ~5.5 ms to ~6.4 ms (≈0.45 µs per converted value). Small results show no measurable
difference. TIME values are left as they are; happy to follow up separately on those.
cloud-sql-mssqlis unit-tested and compile-verified only, since its integration test needs areal Cloud SQL instance. Its
RunSQLis identical to themssqlone and the change to it is thesame, the shared
ConvertToTypeis unit-tested, and this PR wiresRunMSSQLValueTypesTestintoTestCloudSQLMSSQLToolEndpointsso CI exercises it.PR Checklist
CONTRIBUTING.md
bug/issue
before writing your code! That way we can discuss the change, evaluate
designs, and agree on the general idea
review
!if this involve a breaking change — not a breaking change: no configor JSON type changes, though the value encoding for these column types does change, as
described above
🛠️ Fixes #4283