Skip to content

fix(source/mssql,source/cloudsqlmssql): return decimal, money and GUID values as text instead of base64 - #4284

Open
Mordris wants to merge 2 commits into
googleapis:mainfrom
Mordris:fix/mssql-decimal-guid-values
Open

Mordris wants to merge 2 commits into
googleapis:mainfrom
Mordris:fix/mssql-decimal-guid-values

Conversation

@Mordris

@Mordris Mordris commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Description

RunSQL in the mssql and cloud-sql-mssql sources scans every column into any and adds the
value to the row unchanged. go-mssqldb returns DECIMAL, NUMERIC, MONEY and SMALLMONEY values as
their decimal text in a []byte and UNIQUEIDENTIFIER values as their 16 wire bytes, and
encoding/json writes a []byte as base64. So these values reach the client base64-encoded and
an 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's
ColumnType.DatabaseTypeName(). Decimals become their exact decimal text, so there is no float
rounding at any precision; GUIDs become their canonical string via go-mssqldb's own
UniqueIdentifier. Every other value, including BINARY, VARBINARY and IMAGE, is returned
unchanged. DatabaseTypeName() reports DECIMAL for NUMERIC columns too, so one case covers
both. 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/call on /mcp, protocol
2025-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) and decimal(38,10) at full precision, MONEY and SMALLMONEY at
their minimum and maximum, a numeric(5,1) column, and NULL GUIDs; the returned GUID matches
SQL Server's own CAST(... AS char(36)) text, so the byte order is right. int, bigint
(exact, no float rounding), float, datetime2, time, nvarchar, varbinary and bit are
unchanged. go test ./cmd/... ./internal/... passes (426 packages, 0 failures), as does
-race; go vet, gofmt and golangci-lint run are clean. Applying only this PR's test files
on top of unmodified main makes 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-mssql is unit-tested and compile-verified only, since its integration test needs a
real Cloud SQL instance. Its RunSQL is identical to the mssql one and the change to it is the
same, the shared ConvertToType is unit-tested, and this PR wires RunMSSQLValueTypesTest into
TestCloudSQLMSSQLToolEndpoints so CI exercises it.

PR Checklist

  • Make sure you reviewed
    CONTRIBUTING.md
  • Make sure to open an issue as a
    bug/issue
    before writing your code! That way we can discuss the change, evaluate
    designs, and agree on the general idea
  • Ensure you have manually reviewed the entire diff before requesting a
    review
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary) — no new configuration key, so no docs change
  • Make sure to add ! if this involve a breaking change — not a breaking change: no config
    or JSON type changes, though the value encoding for these column types does change, as
    described above

🛠️ Fixes #4283

@Mordris
Mordris requested review from a team as code owners October 18, 2026 11:17
@google-cla

google-cla Bot commented Oct 18, 2026

Copy link
Copy Markdown

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Mordris

Mordris commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

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 "NUMERIC" to the switch, and it is
already there: internal/tools/mssql/mssqlcommon/mssqlcommon.go:40 reads
case "DECIMAL", "NUMERIC", "MONEY", "SMALLMONEY":, the reason it is listed explicitly is in
the doc comment just above, and mssqlcommon_test.go:48 covers a NUMERIC column. So I have
not pushed a change for it.

On CI. cla/google, lint, check-changes, header-check and conventionalcommits.org
all pass — the google-cla comment above is stale, it predates my signing and the check has
been green since. integration-test-pr and toolbox-evals-pr sit at action required, which
I can't clear myself: as a first-time contributor the Cloud Build runs need a maintainer to
approve them. If someone can do that, the rest of the suite can run.

This does change the JSON that existing users receive for the affected column types, which is
the part most worth a second opinion — it's described in the PR body and in #4283. Happy to
rebase or take a different approach.

anubhav756 added a commit that referenced this pull request Nov 16, 2026
… 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.
@Mordris
Mordris force-pushed the fix/mssql-decimal-guid-values branch from c9278e4 to c9459c7 Compare December 2, 2026 14:26

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mssql: DECIMAL, NUMERIC, MONEY and UNIQUEIDENTIFIER values are returned base64-encoded

1 participant