Skip to main content
Glama
tndfame
by tndfame

query_mssql

Execute read-only MSSQL queries to retrieve data from databases. Returns query results with column information, row counts, and preview data for analysis.

Instructions

Run a read-only MSSQL query (SELECT/WITH). Returns columns, rowCount, and sliced rows for preview.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sqlYesRead-only SQL beginning with SELECT or WITH. Parameters as @name.
paramsNoKey-value parameters for @name placeholders in SQL.
limitNoMaximum rows to return (client-side slice).
maxCharsNoMax characters in serialized preview.

Implementation Reference

  • The handler function for the 'query_mssql' tool. It executes a read-only SQL query using the queryReadOnly helper, slices the rows to the specified limit, truncates large JSON previews, and returns a structured success response with columns, row count, and preview rows, or an error response.
    async ({ sql, params, limit, maxChars }) => {
      try {
        const { rows, columns } = await queryReadOnly(sql, params);
        const sliced = rows.slice(0, limit);
        const payload = { columns, rowCount: rows.length, rows: sliced };
        let text = JSON.stringify(payload);
        if (text.length > maxChars) {
          text = text.slice(0, maxChars) + "... (truncated)";
        }
        return createSuccessResponse({
          columns,
          rowCount: rows.length,
          rows: sliced,
        });
      } catch (e: any) {
        return createErrorResponse(`MSSQL query failed: ${e?.message || e}`);
      }
    },
  • Zod schemas defining the input parameters for the query_mssql tool: sql (string), params (optional record), limit (number 1-500 default 100), maxChars (number 1000-200000 default 12000).
    const sqlSchema = z
      .string()
      .min(1)
      .describe(
        "Read-only SQL beginning with SELECT or WITH. Parameters as @name.",
      );
    const paramsSchema = z
      .record(z.any())
      .optional()
      .describe("Key-value parameters for @name placeholders in SQL.");
    const limitSchema = z
      .number()
      .int()
      .min(1)
      .max(500)
      .default(100)
      .describe("Maximum rows to return (client-side slice).");
    const maxCharsSchema = z
      .number()
      .int()
      .min(1000)
      .max(200000)
      .default(12000)
      .describe("Max characters in serialized preview.");
  • src/index.ts:76-76 (registration)
    Top-level registration of the QueryMssql tool instance on the MCP server in the main index file.
    new QueryMssql().register(server);
  • Core helper function that validates the SQL is read-only SELECT/WITH, connects to the MSSQL connection pool, binds parameters, executes the query, and returns rows and column names.
    export async function queryReadOnly(
      sql: string,
      params?: QueryParams,
    ): Promise<{ rows: any[]; columns: string[] }> {
      if (!isReadOnlySelect(sql)) {
        throw new Error("Only read-only SELECT/WITH queries are allowed");
      }
      const pool = await getPool();
      const request = pool.request();
      if (params && typeof params === "object") {
        for (const [k, v] of Object.entries(params)) {
          request.input(k, mapJsToSqlType(v), v);
        }
      }
      const result = await request.query<any>(sql);
      const rows: any[] = (result.recordset as any[]) || [];
      const columns: string[] = rows[0] ? Object.keys(rows[0]) : [];
      return { rows, columns };
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/tndfame/mcp_management'

If you have feedback or need assistance with the MCP directory API, please join our Discord server