MCP Grammar Tools
Allows generating text using OpenAI models constrained by a grammar, using the OpenAI Responses API with custom tool grammar format.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@MCP Grammar ToolsValidate this grammar: start: 'hello' 'world'"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
MCP Grammar Tools
MCP server for validating and testing llguidance grammars (Lark format). Provides grammar validation, batch test execution, and syntax documentation — ideal for iteratively building grammars with AI coding assistants.
Installation
With uvx (recommended)
uvx guidance-lark-mcpWith pip
pip install guidance-lark-mcpFrom source
cd mcp-grammar-tools
pip install -e .Related MCP server: Fast Mermaid Validator MCP
MCP Client Configuration
GitHub Copilot CLI
You can add the server using the interactive /mcp add command or by editing the config file directly. See the Copilot CLI MCP documentation for full details.
Option 1: Interactive setup
In the Copilot CLI, run /mcp add, select Local/STDIO, and enter uvx guidance-lark-mcp as the command.
Option 2: Edit config file
Add the following to ~/.copilot/mcp-config.json:
{
"mcpServers": {
"grammar-tools": {
"type": "local",
"command": "uvx",
"args": ["guidance-lark-mcp"],
"tools": ["*"]
}
}
}This gives you grammar validation and batch testing out of the box. To also enable LLM-powered generation (generate_with_grammar), add ENABLE_GENERATION and your credentials to env:
"env": {
"ENABLE_GENERATION": "true",
"OPENAI_API_KEY": "your-key-here"
}For Azure OpenAI (with Entra ID via az login), use guidance-lark-mcp[azure] and set the endpoint instead:
"args": ["guidance-lark-mcp[azure]"],
"env": {
"ENABLE_GENERATION": "true",
"AZURE_OPENAI_ENDPOINT": "https://your-resource.openai.azure.com/",
"OPENAI_MODEL": "your-deployment-name"
}See Backend Configuration for all supported backends.
After saving, use /mcp show to verify the server is connected.
VS Code
{
"mcpServers": {
"grammar-tools": {
"type": "local",
"command": "uvx",
"args": ["guidance-lark-mcp"],
"env": {
"ENABLE_GENERATION": "true",
"OPENAI_API_KEY": "your-key-here"
},
"tools": ["*"]
}
}
}Claude Desktop
{
"mcpServers": {
"grammar-tools": {
"command": "uvx",
"args": ["guidance-lark-mcp"],
"env": {
"ENABLE_GENERATION": "true",
"OPENAI_API_KEY": "your-key-here"
}
}
}
}Usage
Available Tools
validate_grammar— Validate grammar completeness and consistency using llguidance's built-in validator.{"grammar": "start: \"hello\" \"world\""}run_batch_validation_tests— Run batch validation tests from a JSON file against a grammar. Returns pass/fail statistics and detailed failure info.{ "grammar": "start: /[0-9]+/", "test_file": "tests.json" }Test file format:
[ {"input": "123", "should_parse": true, "description": "Valid number"}, {"input": "abc", "should_parse": false, "description": "Not a number"} ]get_llguidance_documentation— Fetch the llguidance grammar syntax documentation from the official repo.generate_with_grammar(optional, requiresENABLE_GENERATION=true) — Generate text using an OpenAI model constrained by a grammar. Uses the Responses API with custom tool grammar format, so output is guaranteed to conform to the grammar. RequiresOPENAI_API_KEYenvironment variable. See Backend Configuration for Azure and other endpoints.
Backend Configuration
The generate_with_grammar tool uses the OpenAI Python SDK, which natively supports multiple backends via environment variables:
Backend | Required env vars | Optional env vars |
OpenAI (default) |
|
|
Azure OpenAI (API key) |
|
|
Azure OpenAI (Entra ID) |
|
|
Custom endpoint |
|
|
The server auto-detects which backend to use:
If
AZURE_OPENAI_ENDPOINTis set → usesAzureOpenAIclient (with Entra ID or API key)Otherwise → uses
OpenAIclient (readsOPENAI_API_KEYandOPENAI_BASE_URLautomatically)
The server logs which backend it detects on startup.
Example: Azure OpenAI (API key)
{
"mcpServers": {
"grammar-tools": {
"type": "local",
"command": "uvx",
"args": ["guidance-lark-mcp"],
"env": {
"ENABLE_GENERATION": "true",
"AZURE_OPENAI_ENDPOINT": "https://my-resource.openai.azure.com",
"AZURE_OPENAI_API_KEY": "your-azure-key",
"OPENAI_MODEL": "gpt-4.1"
},
"tools": ["*"]
}
}
}Example: Azure OpenAI (Entra ID / keyless)
Requires az login and the azure extra: pip install guidance-lark-mcp[azure]
{
"mcpServers": {
"grammar-tools": {
"type": "local",
"command": "uvx",
"args": ["guidance-lark-mcp[azure]"],
"env": {
"ENABLE_GENERATION": "true",
"AZURE_OPENAI_ENDPOINT": "https://my-resource.openai.azure.com",
"OPENAI_MODEL": "gpt-4.1"
},
"tools": ["*"]
}
}
}Example Workflow
Build a grammar iteratively with an AI assistant:
Start with the spec — paste EBNF rules from a language specification
Write a basic grammar — translate a few rules to Lark format
Validate — use
validate_grammarto check for missing rulesWrite tests — create a JSON test file with sample inputs
Batch test — use
run_batch_validation_teststo find failuresFix & repeat — refine the grammar until all tests pass
Example Grammars
The examples/ directory includes sample grammars built using these tools, with Lark grammar files, test suites, and documentation:
GraphQL — executable subset of the GraphQL spec (queries, mutations, fragments, variables)
Troubleshooting
Server fails to connect in Copilot CLI / VS Code?
MCP clients like Copilot CLI only show "Connection closed" when a server crashes on startup. To see the actual error, run the server directly in your terminal:
uvx guidance-lark-mcpOr with generation enabled:
ENABLE_GENERATION=true OPENAI_API_KEY=your-key uvx guidance-lark-mcpCommon issues:
Missing credentials —
ENABLE_GENERATION=truewithout a validOPENAI_API_KEYorAZURE_OPENAI_ENDPOINT. The server will still start and serve validation tools;generate_with_grammarwill return a descriptive error.Azure Entra ID — make sure you've run
az loginand are usingguidance-lark-mcp[azure](not the base package).Slow first start —
uvxneeds to resolve and install dependencies on first run, which may exceed the MCP client's connection timeout. Runuvx guidance-lark-mcponce manually to warm the cache.Updating to a new version —
uvxcaches packages, so after a new release you may need to clear the cache and restart your MCP client:uv cache clean guidance-lark-mcp
Development
git clone https://github.com/guidance-ai/guidance-lark-mcp
cd guidance-lark-mcp
uv sync
uv run pytest tests/ -qMaintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Latest Blog Posts
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
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/guidance-ai/guidance-lark-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server