/langium-ai:lai-gen-mcpfrom langium-ai
Generates an MCP server that exposes a Langium DSL's parser and validator as a validate tool, enabling MCP-compatible clients to check DSL code and receive diagnostics.
Ships in langium-ai. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
This guide instructs an agent on how to generate a Model Context Protocol (MCP) server for an arbitrary Langium-based DSL. The generated server exposes a validate tool that accepts DSL source code and returns diagnostics (errors, warnings, hints, information) by running the input through the DSL's actual Langium parser and validator. Any MCP-compatible client (Claude Code, Cursor, VS Code, etc.) can then validate DSL code on the fly.
You may also use the lai and langium skills to achieve a better understanding of langium-ai and the Langium-based DSL in question.
lai init and a descriptor existsThe target Langium project must have:
.langium file) and generated TypeScript artifacts (ast.ts, grammar.ts, module file)create<Name>Services function in its module file (e.g., createMyDslServices in src/my-dsl-module.ts). This is the entry point for all language services.Gather the following from the project before generating:
language.descriptor.yml) or lai.config.jsonc — to find the grammar path, service paths, and language namecreate<Name>Services(...) function. Typically located at src/<name>-module.ts or src/language/<name>-module.ts. The descriptor's services.module field points to it if present; otherwise search for createServices or create.*Services in the src/ directory.LanguageMetaData export (usually in generated/ast.ts or generated/module.ts) tells you the language's file extensionsBefore generating any files, determine where the mcp/ folder should be placed. The location depends on whether the project is a monorepo or a single-package project.
Check for monorepo indicators in the project root:
package.json workspaces field — if the root package.json has a workspaces array (or workspaces.packages), the project is a monorepopnpm-workspace.yaml — presence of this file indicates a pnpm monorepolerna.json — presence of this file indicates a Lerna monoreponx.json — presence of this file indicates an Nx monorepoIf none of these are present, treat the project as a single-package project.
The default output location is mcp/ at the project root:
my-dsl-project/
src/
mcp/ <-- generated here
mcp-server.ts
tsconfig.json
package.json
In a monorepo, the mcp/ folder should be placed inside the specific Langium package, not at the monorepo root. Use the lai.config.jsonc or language.descriptor.yml location to determine which package contains the DSL. For example:
my-monorepo/
packages/
my-dsl/
src/
mcp/ <-- generated here, alongside the DSL package
mcp-server.ts
tsconfig.json
package.json
other-package/
package.json <-- monorepo root, NOT here
If lai.config.jsonc is at the root of a monorepo but the Langium grammar and module live in a sub-package, prefer placing mcp/ inside that sub-package so that imports resolve correctly relative to the DSL source.
Before proceeding with file generation, always ask the user to confirm the proposed output location. Present the detected location and explain why it was chosen:
mcp/ at the project root. Does that location work for you?"packages/my-dsl/mcp/ alongside the DSL package. Does that location work for you, or would you prefer a different path?"Wait for the user's confirmation or alternative path before generating any files.
The agent should produce:
mcp/mcp-server.ts — the MCP server source file (inside the confirmed location)mcp/tsconfig.json — a minimal tsconfig for the MCP serverpackage.json — with required dependencies added (or instructions to install them). In a monorepo, update the DSL package's package.json, not the root.Find the module file in the Langium project. It exports a function like:
export function createMyDslServices(context: DefaultSharedCoreModule): {
shared: LangiumSharedServices;
MyDsl: MyDslServices;
}
Note:
createMyDslServices)MyDsl). This is the key you pass to LangiumEvaluator.NodeFileSystem or EmptyFileSystem — the MCP server should use EmptyFileSystem since it validates in-memory strings, not files on diskThe following npm packages are required. Add them to the project's package.json:
npm install @modelcontextprotocol/sdk langium-ai-tools zod
@modelcontextprotocol/sdk (^1.25.1) — the MCP protocol SDKlangium-ai-tools — provides LangiumEvaluator for parsing and validating DSL code (treats langium as a peer dependency, supports 4.x and up)zod (^3.25 || ^4.0) — schema validation for tool input, required by the MCP SDKThe project should already have langium as a dependency. If not, install it too:
npm install langium
In a monorepo, run these install commands from the DSL package directory (or use npm install -w packages/my-dsl from the root).
mcp/mcp-server.tsCreate the MCP server file. Use the following template, replacing the placeholder values:
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { LangiumEvaluator } from 'langium-ai-tools';
import { EmptyFileSystem } from 'langium';
import { z } from 'zod';
// REPLACE: import your DSL's service creation function
import { create__DSL_NAME__Services } from '../src/__module_path__';
// initialize the MCP server
const server = new McpServer({
name: '__dsl-name__-mcp-server',
version: '1.0.0',
});
// create language services and the evaluator
const services = create__DSL_NAME__Services(EmptyFileSystem).__ServiceKey__;
const evaluator = new LangiumEvaluator(services);
// register the validate tool
server.registerTool(
'__dsl-name__-syntax-checker',
{
title: '__DSL Name__ Syntax Checker',
description: 'Validates __DSL Name__ code and returns diagnostics (errors, warnings, hints).',
inputSchema: { code: z.string() },
},
async ({ code }) => {
const result = await validateCode(code);
return {
content: [
{
type: 'text',
text: result ?? 'The provided code has no issues.',
},
],
};
},
);
async function validateCode(code: string): Promise<string | undefined> {
const evalResult = await evaluator.evaluate(code);
if (evalResult.data) {
const diagnostics = evalResult.data.diagnostics;
if (diagnostics.length > 0) {
return diagnostics
.map(
(d) =>
`${severityText(d.severity)}: ${d.message} at line ${d.range.start.line + 1}, column ${d.range.start.character + 1}`,
)
.join('\n');
}
}
return undefined;
}
function severityText(severity: number | undefined): string {
switch (severity) {
case 1:
return 'Error';
case 2:
return 'Warning';
case 3:
return 'Information';
case 4:
return 'Hint';
default:
return 'Unknown';
}
}
// start the server with stdio transport
const transport = new StdioServerTransport();
await server.connect(transport);
| Placeholder | Replace With | Example |
|---|---|---|
__DSL_NAME__ | PascalCase name from the service creation function | MyDsl |
__dsl-name__ | Kebab-case name for identifiers and tool names | my-dsl |
__DSL Name__ | Human-readable name for descriptions | My DSL |
__module_path__ | Relative path from mcp/ to the module file (without .ts) | language/my-dsl-module |
__ServiceKey__ | The property key on the services object | MyDsl |
Some Langium projects require workspace initialization before validation works correctly (e.g., for built-in libraries or preloaded documents). If the project has built-in files or a custom workspace manager, add initialization after creating services:
const shared = create__DSL_NAME__Services(EmptyFileSystem).shared;
await shared.workspace.WorkspaceManager.initializeWorkspace([]);
Check if the project's tests or evaluations call initializeWorkspace — if they do, include it in the MCP server.
mcp/tsconfig.jsonCreate a minimal tsconfig for the MCP server directory:
{
"compilerOptions": {
"target": "ESNext",
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "./dist",
"rootDir": ".",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"declaration": true
},
"include": ["."],
"exclude": ["node_modules", "dist"]
}
If the project already has a tsconfig.base.json at the root, extend it instead:
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "./dist"
},
"include": ["."],
"exclude": ["node_modules", "dist"]
}
In a monorepo, adjust the extends path relative to the mcp/ folder's actual location (e.g., ../../tsconfig.base.json if the mcp folder is inside a sub-package).
Add a script to package.json so the server can be started easily:
{
"scripts": {
"mcp:start": "npx tsx mcp/mcp-server.ts"
}
}
Using tsx allows running TypeScript directly without a separate compilation step. If the project prefers compiled output, add a build step instead:
{
"scripts": {
"mcp:build": "tsc -p mcp/tsconfig.json",
"mcp:start": "node mcp/dist/mcp-server.js"
}
}
Provide the user with a configuration snippet they can add to their MCP client. The exact format depends on the client:
claude_desktop_config.json or .mcp.json){
"mcpServers": {
"__DSL Name__ MCP": {
"command": "npx",
"args": ["tsx", "mcp/mcp-server.ts"],
"cwd": "/absolute/path/to/project"
}
}
}
.cursor/mcp.json){
"mcpServers": {
"__DSL Name__ MCP": {
"command": "npx",
"args": ["tsx", "mcp/mcp-server.ts"],
"cwd": "/absolute/path/to/project"
}
}
}
Replace /absolute/path/to/project with the actual project root (or DSL package root in a monorepo).
After generation, verify the server works:
npx tsx mcp/mcp-server.ts and confirm it starts without errors (it will block waiting for stdio input; Ctrl+C to exit)import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
const transport = new StdioClientTransport({
command: 'npx',
args: ['tsx', 'mcp/mcp-server.ts'],
});
const client = new Client({ name: 'test-client', version: '1.0.0' });
await client.connect(transport);
const tools = await client.listTools();
console.log('Available tools:', tools.tools.map(t => t.name));
// test with valid code
const result = await client.callTool({
name: '__dsl-name__-syntax-checker',
arguments: { code: '<paste a valid example program here>' },
});
console.log('Result:', result.content);
process.exit(0);
The validate tool accepts a code string and returns one of:
"The provided code has no issues." — the input parsed and validated successfullySeverity: Message at line N, column M
Where severity is one of: Error, Warning, Information, Hint.Diagnostics come from two sources:
This means the MCP tool respects all custom validation rules, scoping rules, and linking behavior defined in the Langium project — it is not just a syntax checker but a full language validator.
After generating the base server, the user may want to extend it:
The MCP SDK supports registering multiple tools. Common additions:
The base server validates a single code string. For languages that support imports or multi-file projects, the server can be extended to accept multiple files or a workspace context.
Some users may want to filter diagnostics by severity (e.g., only return errors, not warnings). This can be added as an optional input parameter:
inputSchema: {
code: z.string(),
minSeverity: z.number().min(1).max(4).optional().describe('Minimum severity to report (1=Error, 2=Warning, 3=Info, 4=Hint). Defaults to 4 (all).')
}
| Artifact | Purpose |
|---|---|
| Descriptor | Tells the agent where to find the module file, grammar, and services |
| System prompt | Teaches an LLM how to write valid DSL code |
| MCP server | Lets an LLM validate DSL code it has written, closing the feedback loop |
| Evaluations | Batch-test LLM output quality; the MCP server enables real-time single-input validation |
The MCP server complements the system prompt: the prompt teaches the LLM how to write correct code, and the MCP server lets it verify its output. Together they form a generation-validation loop.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub eclipse-langium/langium-ai --plugin langium-ai7 skills in this plugin
/laiGuide for using the langium-ai (LAI) CLI to generate language descriptors, synthesize system prompts, run evaluations, and iteratively refine AI-powered tooling in Langium projects. Use when working with lai commands, descriptors, or evaluation files./lai-gen-descriptorGenerate or refine a language descriptor for a Langium DSL project. Bootstraps a new descriptor via `lai gen descriptor` if none exists, then guides refinement of paths, services, examples, documentation, and structure./lai-gen-evalsExpand and refine the evaluation suite for a Langium DSL project. Generates comprehensive eval files that cover syntactic correctness, semantic validity, user intent matching, edge cases, and language understanding./lai-gen-language-skillSkill for generating a skill for understanding a specific Langium-based DSL. Used in cooperation with the lai & langium skills for understanding./lai-gen-syspromptGenerate or refine a system prompt for a Langium DSL project. Bootstraps a new system prompt via `lai gen sysprompt` if none exists, then guides targeted refinement based on evaluation results and language changes./langiumBuild, fix, and improve Langium 4.x languages. Recipes, best practices, and troubleshooting for `.langium` grammars, scoping and cross-references, validation, LSP features (formatting, semantic tokens, code actions, hover, completion), code generators, `langium/test` tests, `langium-config.json`, and multi-language projects. Use when working in any project that depends on `langium` or `langium-cli`, or when a Langium language misbehaves (parse errors, unresolved references, missing diagnostics or editor features).Other plugins' skills for the same job