Skip to content

ai.mcp.mcpgen #

MCP Generator

An implementation of the Model Context Protocol (MCP) server for V language operations. This server uses the Standard Input/Output (stdio) transport as described in the MCP documentation.

Features

The server supports the following operations:

  1. test - Run V tests on a file or directory
  2. run - Execute V code from a file or directory
  3. compile - Compile V code from a file or directory
  4. vet - Run V vet on a file or directory

Usage

Building the Server

v -gc none -stats -enable-globals -n -w -cg -g -cc tcc /Users/despiegk/code/github/freeflowuniverse/herolib/lib/mcp/v_do

Using the Server

The server communicates using the MCP protocol over stdio. To send a request, use the following format:

Content-Length: <length>

{"jsonrpc":"2.0","id":"<request-id>","method":"<method-name>","params":{"fullpath":"<path-to-file-or-directory>"}}

Where:- <length> is the length of the JSON message in bytes

  • <request-id> is a unique identifier for the request
  • <method-name> is one of: test, run, compile, or vet
  • <path-to-file-or-directory> is the absolute path to the V file or directory to process

Example

Request:

Content-Length: 85

{"jsonrpc":"2.0","id":"1","method":"test","params":{"fullpath":"/path/to/file.v"}}

Response:

Content-Length: 245

{"jsonrpc":"2.0","id":"1","result":{"output":"Command: v -gc none -stats -enable-globals -show-c-output -keepc -n -w -cg -o /tmp/tester.c -g -cc tcc test /path/to/file.v\nExit code: 0\nOutput:\nAll tests passed!"}}

Methods

test

Runs V tests on the specified file or directory.

Command used:

v -gc none -stats -enable-globals -show-c-output -keepc -n -w -cg -o /tmp/tester.c -g -cc tcc test ${fullpath}

If a directory is specified, it will run tests on all .v files in the directory (non-recursive).

run

Executes the specified V file or all V files in a directory.

Command used:

v -gc none -stats -enable-globals -n -w -cg -g -cc tcc run ${fullpath}

compile

Compiles the specified V file or all V files in a directory.

Command used:

cd /tmp && v -gc none -enable-globals -show-c-output -keepc -n -w -cg -o /tmp/tester.c -g -cc tcc ${fullpath}

vet

Runs V vet on the specified file or directory.

Command used:

v vet -v -w ${fullpath}

Constants #

const command = cli.Command{
	sort_flags: true
	name:       'mcpgen'
	// execute:     cmd_mcpgen
	description: 'will list existing mdbooks'
	commands:    [
		cli.Command{
			name:        'start'
			execute:     cmd_start
			description: 'start the MCP server'
		},
	]
}

fn argument_decode_stmt #

fn argument_decode_stmt(param code.Param) string

fn array_to_mcp_tool_contents #

fn array_to_mcp_tool_contents[U](array []U) []mcp.ToolContent

fn new_mcp_server #

fn new_mcp_server(v &MCPGen) !&mcp.Server

fn result_to_mcp_tool_content #

fn result_to_mcp_tool_content[T](result T) mcp.ToolContent

fn result_to_mcp_tool_contents #

fn result_to_mcp_tool_contents[T](result T) []mcp.ToolContent

struct FunctionPointer #

struct FunctionPointer {
	name        string // name of function
	module_path string // path to module
}

struct MCPGen #

@[heap]
struct MCPGen {}

fn (MCPGen) create_mcp_tool #

fn (d MCPGen) create_mcp_tool(function code.Function, types map[string]string) !mcp.Tool

create_mcp_tool parses a V language function string and returns an MCP Tool struct function: The V function string including preceding comments types: A map of struct names to their definitions for complex parameter types

fn (MCPGen) create_mcp_tool_code #

fn (d &MCPGen) create_mcp_tool_code(function_name string, module_path string) !string

create_mcp_tool_code receives the name of a V language function string, and the path to the module in which it exists. returns an MCP Tool code in v for attaching the function to the mcp server

fn (MCPGen) create_mcp_tool_code_tool_handler #

fn (d &MCPGen) create_mcp_tool_code_tool_handler(arguments map[string]Any) !mcp.ToolCallResult

fn (MCPGen) create_mcp_tool_const_tool_handler #

fn (d &MCPGen) create_mcp_tool_const_tool_handler(arguments map[string]Any) !mcp.ToolCallResult

fn (MCPGen) create_mcp_tool_handler #

fn (d &MCPGen) create_mcp_tool_handler(function code.Function, types map[string]string, result_ string) !string

create_mcp_tool parses a V language function string and returns an MCP Tool struct function: The V function string including preceding comments types: A map of struct names to their definitions for complex parameter types result: The type of result of the create_mcp_tool function. Could be simply string, or struct {...}

fn (MCPGen) create_mcp_tool_handler_tool_handler #

fn (d &MCPGen) create_mcp_tool_handler_tool_handler(arguments map[string]Any) !mcp.ToolCallResult

Tool handler for the create_mcp_tool_handler function

fn (MCPGen) create_mcp_tools_code #

fn (d &MCPGen) create_mcp_tools_code(function_pointers []FunctionPointer) !string

create_mcp_tool_code receives the name of a V language function string, and the path to the module in which it exists. returns an MCP Tool code in v for attaching the function to the mcp server function_pointers: A list of function pointers to generate tools for

fn (MCPGen) create_mcp_tools_code_tool_handler #

fn (d &MCPGen) create_mcp_tools_code_tool_handler(arguments map[string]Any) !mcp.ToolCallResult