
JSON Contracts
io.github.json-contracts由 GitHub 账号 json-contracts 发布v0.1.2更新于 Oct 25, 2026
JSON contract registry and validator for structured LLM and agent outputs.
概览
本地 MCP 服务器,提供受 Git 管理的 JSON 契约与 JSON Schema 校验,让助手把自然语言转换为符合 schema 的 JSON。
- 可以做什么
- 列出并读取本地文件夹中加载的 JSON 契约,包括 schema、规则、示例和哈希值,并可在修改后重新加载。按输入和上下文获取创建契约,供助手自己的模型生成 JSON。携带当前 JSON 获取编辑契约,让模型返回完整的更新对象。用契约 schema 校验助手生成的 JSON,并返回字段级错误。校验失败时获取带确定性提示的修复契约。查看服务器状态和已加载契约以便排查配置。
- 适用场景
- 适用于应用或智能体需要从自然语言稳定产出结构化 JSON,并希望把输出结构、规则和示例放在受版本控制的文件中而不是提示词里的场景。适合已自行选择模型、希望在旁边加上校验与修复指引的团队。
- 运行要求
- 通过 npm 包 json-contracts 以 stdio 在本地运行(npx 或全局安装),需要 Node.js。需要一个存放 JSON 契约文件的文件夹,由环境变量 JSON_CONTRACTS_DIR 指定(默认 ./json-contracts)。服务器本身不需要 API 密钥或 LLM 提供商访问权限。
安装
在 SourceWeft 中
- 打开 控制台中的 JSON Contracts,将其添加到工作区。
- 为需要使用其工具的对话启用该服务。
Desktop only,通过 STDIO。 STDIO 服务会启动本地进程,因此需要 SourceWeft 桌面宿主。
其他 MCP 客户端
参照 仓库 中的启动说明。
README
JSON Contracts MCP Server
JSON Contracts is a local MCP contract server for natural-language-to-JSON workflows. It gives agents Git-controlled JSON contracts, rules, examples, and JSON Schema validation tools so any model can reliably convert natural-language requests into schema-valid JSON.
The MCP server does not call an LLM provider. The MCP server does not need API keys. The MCP server does not use BAML, LangChain, Markdown, or a DSL. The MCP server does not use MCP sampling. The MCP server does not generate JSON by itself.
The optional Studio is a separate package/repo for live demos and local contract testing. The MCP stdio server itself does not call LLM providers.
Your user provides natural language.
Your agent chooses the model.
Your agent performs the natural-language-to-JSON conversion.
json-contracts provides the contract and validates the result.
Correct mental model
Not this:
This:
Like TypeScript:
With json-contracts:
Architecture
The MCP server never performs natural-language-to-JSON conversion itself. It only provides contracts, validation, and repair guidance.
Install
Install globally:
Or run without installing:
The npm package is json-contracts; the installed CLI binary is still json-contracts.
30-second setup
Create a starter contract folder and validate it:
Then add the MCP config below to your agent host. Point JSON_CONTRACTS_DIR at the json-contracts folder that init created.
Validate contracts in CI without starting MCP:
By default, the server starts as a local stdio MCP server and loads contracts from:
MCP config
Adding a new behavior only requires adding a new .json file to the contracts folder. No MCP config change is required.
MCP host config snippets
Most MCP hosts use the same stdio shape. Adapt paths for your machine and point JSON_CONTRACTS_DIR at your app-owned contracts folder.
Claude Desktop
Cursor / Windsurf / VS Code MCP-compatible hosts
Continue or generic stdio MCP clients
If npx startup is too slow or your host requires explicit executables, install globally and use:
Adopt in your agent/app
Use json-contracts as a local contract/validation tool beside the model your app already uses.
1. Add the MCP server
If your agent host supports MCP, add the server config above and point JSON_CONTRACTS_DIR at the contracts folder your app owns.
If your app has its own agent runtime, connect to the same MCP stdio server and call the tools directly. The important part is the flow, not the host.
2. Write one contract per JSON behavior
Create files such as:
Each file contains:
schemafor the final JSON shaperulesfor app-specific mapping behaviorexamplesfor model guidance- optional
operationsfor create/edit behavior
3. Pass app/system variables as context
Do not hide runtime variables in the user prompt. Pass them as context:
json-contracts passes context through unchanged. Contracts decide how to use it through rules/examples. The final JSON still must match the schema.
4. Give your agent this tool policy
Use this as the system/developer instruction for your agent:
5. Runtime loop in your app
At request time, your app should do:
The MCP server does not call the model and does not mutate output. Your app/agent remains in control of model choice, provider keys, context, and business defaults.
Contract files
Each contract is one .json file in json-contracts/.
The contract name is derived from the filename:
There is no manifest file and no manually maintained resources file. Git handles versioning.
Contract shape
Rules:
schemais required and must be valid JSON Schema. Omit$schemafor the default 2020-12 validator, or set$schemato draft-07 or 2020-12 explicitly.descriptionis optional but recommended.rulesis optional and defaults to[].examplesis optional and defaults to[]. Exampleoutputvalues must validate againstschema.operationsis optional and defaults to enabledcreateandeditoperations. Operation metadata belongs at the top level, not inside the JSON Schema.nameis optional, but the filename is the source of truth.- A
versionfield is rejected; use Git for versioning. - Contract files are plain JSON.
Included example contracts
The default json-contracts/ folder includes examples from different app categories where teams commonly rebuild the same AI glue code:
Each one uses the same MCP tools: read the contract, let the model create or edit JSON, validate it, and repair if needed. New apps should not need a new bespoke prompt framework just to get reliable JSON.
MCP resources
Every loaded contract is exposed dynamically as:
Examples:
resources/list returns all loaded contracts. resources/read returns the normalized full contract JSON.
Stable MCP tools
list_contracts
Input:
Output:
read_contract
Input:
Returns the selected contract's description, rules, operations, schema, examples, contractHash, and schemaHash.
Hashes are deterministic SHA-256 values over the normalized contract/schema payload. They are for logging, cache keys, and audit trails; contract files still use Git for versioning.
get_json_contract
Input:
Output:
The agent/model uses this contract to produce JSON. The MCP server does not produce it.
get_edit_contract
Input:
Output:
The agent/model uses this edit contract to return the complete updated JSON object. The MCP server first validates currentJson against the selected contract, and the agent should validate the edited object with validate_json afterward.
Context and system variables
context is an intentional pass-through object for app/system variables that should help the model interpret the user's input.
Examples:
json-contracts does not interpret, normalize, or validate context against a separate schema. It returns the object unchanged in the contract payload so the model can use it with the contract rules and output schema.
Important behavior:
- Put external variables in
context, not by appending hidden text toinput. - The final JSON must still match the contract schema.
- Context fields should not appear in the final JSON unless the contract schema allows them.
- Contracts can define how to use context through
rules, examples, and schema shape. - Relative values such as "this summer" or "last week" should be resolved by the model using whatever date/time/location context the app provides.
validate_json
Input:
Success:
Failure:
valid is never true unless Ajv validates the JSON against the contract schema.
get_repair_contract
Input:
Output:
The agent/model uses this repair contract to produce corrected JSON. The MCP server does not repair by calling a model. When validation errors are available, instructions also includes deterministic field-specific repair hints such as adding missing required fields, removing extra fields, or choosing allowed enum values.
status
Input:
Output:
Use this to debug host configuration, loaded contracts, and the exact contract/schema hashes in use.
reload_contracts
Input:
Output:
Optional MCP prompts
The server also exposes reusable prompt helpers for MCP hosts that support prompts:
json_contract_promptedit_contract_promptrepair_contract_prompt
These prompts only render contract text for the agent/model. They do not call a model and they do not generate JSON inside the MCP server.
Correct flow
User:
Agent calls:
MCP returns schema, rules, examples, instructions, and input.
Agent/model produces:
Agent calls:
MCP returns:
If invalid, the agent calls get_repair_contract, uses its own model to repair, and calls validate_json again.
Edit flow
For existing JSON, the agent calls get_edit_contract with the current JSON plus a natural-language change request:
The model returns the complete edited object:
Then the agent validates that final edited object with validate_json.
Contract validation and linting CLI
Use the CLI in CI or pre-commit checks without starting an MCP host:
validate loads every .json contract, checks the contract shape, validates the JSON Schema, and validates example outputs against the schema. It exits nonzero if any contract is invalid.
For advisory checks:
lint includes the same validation checks, then prints generic schema-quality warnings such as empty schemas, missing examples, object schemas without additionalProperties:false, or broad additionalProperties:true. --strict exits nonzero when warnings are found.
Both commands support machine-readable output:
Minimal app integration example
A tiny Node MCP client is included at examples/node-client. It shows the app-side loop:
It intentionally does not call an LLM provider. Replace its placeholder getModelJson() with your own model call.
Studio
The local web Studio is intentionally not bundled with this MCP package. It lives in a separate repo/package so the MCP server stays small, local, and focused.
Use the Studio when you want a browser UI for testing contracts, provider-backed demos, manual repair loops, or drafting new contract files.
For a quick demo with starter contracts:
Repository:
Pi local integration
This repo also includes a project-local Pi extension at .pi/extensions/json-contracts-mcp.ts. It starts the local MCP stdio server and exposes the server tools to Pi as jc_* tools for manual testing.
From this repo on Windows PowerShell:
Then try:
See docs/pi-integration.md for the full setup and test prompts.
Documentation site and content strategy
The docs/ folder can be published directly with GitHub Pages from the /docs branch folder setting.
docs/index.mdis the docs landing page.docs/github-pages.mdexplains how to enable GitHub Pages.docs/content-marketing-plan.mdcontains the short-form video/content plan for positioningjson-contractsaround structured output, MCP validation, and production-safe JSON flows.
Git-controlled behavior
The json-contracts/ folder is the product surface.
- Add behavior by adding a new
.jsonfile. - Edit behavior by editing an existing
.jsonfile. - Review behavior through Git pull requests.
- Roll back behavior through Git.
No provider keys, SDKs, sampling, or MCP config changes are required when contracts change.
Environment variables
Studio-only LLM provider variables are documented in the separate json-contracts-studio repo. The MCP stdio server does not use them.
In stdio mode, logs are written to stderr only. The server never writes logs to stdout.
Security notes
The MCP server in json-contracts:
- never calls an LLM provider
- never uses provider API keys
- never performs MCP sampling
- never executes anything from contract files
- never uses
eval - never imports or executes code from contract files
- never logs full user input unless
DEBUG=true - validates MCP tool inputs with Zod
- enforces contract file, schema, and examples limits
- rejects unsafe contract names and resource URIs
- prevents path traversal
The optional separate Studio can call LLM providers only when you configure a key in its local UI or .env file; those provider calls are not part of the MCP stdio server.
Licensing and trademarks
Project owner: Harry Giunta.
Licensing model:
- Runtime code, docs, examples, tests, and project infrastructure are licensed under Apache-2.0. See
LICENSEandNOTICE. - Official starter contracts in
json-contracts/are licensed under Apache-2.0 OR MIT, at your option. Seejson-contracts/LICENSE.md. - Third-party or marketplace contract packs may use creator-selected licenses. They should include their own license file or license metadata and must not imply official status unless approved.
The project name json-contracts is subject to the trademark policy in TRADEMARKS.md. Copyright licenses do not grant trademark rights.
See CONTRIBUTING.md for contribution licensing and project guidelines.
Development
Run the published package as an MCP stdio server:
Or from this repository:
Docker
Docker support is optional. A sample Dockerfile is included for packaging the stdio server with its default contracts.
来源:README.md,提交 68e8536
工具
0版本历史
1- v0.1.2最新Sep 16, 2026
评分
已满足适用检查项的 40%
该评分衡量此收录条目的完整程度,依据注册中心条目、端点与公开来源计算得出。它不是对服务器本身的评价,也不表示服务器安全或当前可用:可连通性是带日期的检查结果,评分也不包含安装量或用户评价。
- 近 30 天内可连通25 分
请保持端点能够响应 MCP 握手;SourceWeft 每 30 天探测一次。
- 已声明认证方式10 分
请在注册中心条目中声明凭据请求头,或保持端点能够响应探测,以便判定其认证方式。
- 已列出工具15 分
请让服务器在无凭据时响应 tools/list,或发布可在沙箱中无凭据启动的软件包。
- 描述必需15 分
- 图标10 分
- 文档15 分
- 许可证10 分
请在仓库或注册中心条目中声明许可证。