googleapis/mcp-toolbox · error

unable to execute query: %w

Error message

unable to execute query: %w

What it means

This error wraps a failure from `QueryContext` in the MSSQL source's `RunSQL`. It means the SQL statement itself failed to execute — syntax errors, missing tables/columns, permission denials, timeouts, or connection drops during execution.

Source

Thrown at internal/sources/mssql/mssql.go:116

func (s *Source) SourceType() string {
	// Returns Cloud SQL MSSQL source type
	return SourceType
}

func (s *Source) ToConfig() sources.SourceConfig {
	return s.Config
}

func (s *Source) MSSQLDB() *sql.DB {
	// Returns a Cloud SQL MSSQL database connection pool
	return s.Db
}

func (s *Source) RunSQL(ctx context.Context, statement string, params []any) (any, error) {
	results, err := s.MSSQLDB().QueryContext(ctx, statement, params...)
	if err != nil {
		return nil, fmt.Errorf("unable to execute query: %w", err)
	}
	defer results.Close()

	cols, err := results.Columns()
	// If Columns() errors, it might be a DDL/DML without an OUTPUT clause.
	// We proceed, and results.Err() will catch actual query execution errors.
	// 'out' will remain an empty slice if cols is empty or err is not nil here.
	out := []any{}
	if err == nil && len(cols) > 0 {
		// create an array of values for each column, which can be re-used to scan each row
		rawValues := make([]any, len(cols))
		values := make([]any, len(cols))
		for i := range rawValues {
			values[i] = &rawValues[i]
		}

		for results.Next() {
			scanErr := results.Scan(values...)

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Run the statement in SSMS/Azure Data Studio to reproduce the exact server error
  2. Unwrap the error and inspect mssql.Error number/state for the server-side cause
  3. Check user permissions on the target database/objects
  4. Increase the context timeout for long-running queries
  5. Validate parameter count and types match the statement's placeholders

Example fix

// before (wrong schema)
stmt := "SELECT * FROM users"
// after
stmt := "SELECT * FROM dbo.users"
Defensive patterns

Strategy: try-catch

Type guard

func isSqlServerErr(err error) bool {
    var se mssql.Error
    return errors.As(err, &se)
}

Try / catch

res, err := src.RunSQL(ctx, stmt, nil)
if err != nil {
    var se mssql.Error
    if errors.As(err, &se) {
        return fmt.Errorf("SQL Server error %d: %s", se.Number, se.Message)
    }
    if errors.Is(err, context.DeadlineExceeded) {
        return fmt.Errorf("query timed out: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling RunSQL with a syntactically invalid statement; referencing non-existent tables or columns; selecting from a database the user lacks rights on; query timeout via context cancellation; parameters count/type mismatch.

Common situations: Typos in T-SQL; querying a table that exists in another database/schema; running DDL without permissions; context deadline exceeded on long queries; stale connections after server failover.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/ea3f68d7403a8cac. Report an issue: GitHub.