从 SQL 生成到工具选择:一种面向领域的 MCP 服务器模式
文章背景与核心概要
随着大语言模型(LLM)代理日益依赖模型上下文协议(MCP)来访问企业数据,许多数据库服务器倾向于提供单一的通用 SQL 执行工具以实现最大灵活性。然而,这种做法往往导致模型在处理复杂查询时表现不佳,且增加了计算成本。
本文提出了一种“面向领域的工具模式”(Domain-Oriented Tooling Pattern),旨在改变传统的查询时 SQL 生成方式。该模式通过将复杂的模式导航、表连接和业务规则封装在服务器端的参数化查询中,让 LLM 从精心设计的领域工具集中进行选择。研究表明,这种方法不仅能显著提升模型在处理常规数据请求时的准确性,还能通过“模型降级”(Model Demotion)策略,使用更小、更经济的模型实现同等甚至更优的性能,从而大幅降低运营成本。
摘要
Agents built on Large Language Models (LLMs) increasingly reach enterprise data through the Model Context Protocol (MCP), and many MCP database servers maximize flexibility by exposing a single generic SQL execution tool. This paper proposes the Domain-Oriented Tooling Pattern: instead of generating SQL at query time, the model selects from a small set of domain-aligned tools whose parameterized queries encapsulate schema navigation, joins and business rules on the server side. We formalize the pattern around three architectural invariants and introduce Model Demotion, the observation that replacing SQL synthesis with intent classification lowers the model tier required to serve routine requests. As a reference implementation we present MCP Blueprint, an open-source framework in which domain tools are defined declaratively as YAML metadata plus external parameterized SQL files. We evaluate the pattern with a public reproducibility benchmark comparing three MCP server designs - raw SQL execution, a thin generic tool pack, and a verticalized domain pack - on four local models (3B-8B) across seventeen customer-facing tasks over the Sakila database (609 completed cells; temperature 0; three repetitions per cell). The verticalized pack reaches a pooled mean score of 0.939 versus 0.666 for raw SQL and 0.605 for the generic pack; the smallest model improves from 0.583 to 0.929, matching or exceeding every larger configuration while cutting cost per correct answer by an order of magnitude. All harness code, prompts, gold answers, frozen packs and per-cell results are publicly available.
基于大语言模型(LLM)的代理正越来越多地通过模型上下文协议(MCP)访问企业数据,许多 MCP 数据库服务器为了追求灵活性,倾向于仅暴露单一的通用 SQL 执行工具。本文提出了“面向领域的工具模式”:模型不再在查询时生成 SQL,而是从一组小型、面向领域的工具中进行选择,这些工具的参数化查询在服务器端封装了模式导航、表连接和业务规则。我们围绕三个架构不变性对该模式进行了形式化,并引入了“模型降级”概念,即观察到用意图分类取代 SQL 合成可以降低处理常规请求所需的模型层级。作为参考实现,我们展示了 MCP Blueprint,这是一个开源框架,其中领域工具通过 YAML 元数据和外部参数化 SQL 文件进行声明式定义。我们通过一个公开的可复现基准测试评估了该模式,比较了三种 MCP 服务器设计(原始 SQL 执行、精简通用工具包和垂直领域工具包),在 Sakila 数据库上针对 17 项面向客户的任务对四种本地模型(3B-8B 参数)进行了测试(共 609 个完成单元;温度设为 0;每个单元重复三次)。结果显示,垂直领域工具包的汇总平均得分为 0.939,远高于原始 SQL 的 0.666 和通用工具包的 0.605;最小的模型得分从 0.583 提升至 0.929,在匹配或超越所有更大配置模型的同时,将每个正确答案的成本降低了一个数量级。所有测试代码、提示词、标准答案、固化工具包及各单元结果均已公开。