← Back to Official Package Registry
Tier 3: AI, Vector RAG & DataFrames • Model Context Protocolv1.0.0 • 34 KB • Ed25519 Verified

ai_mcp@1.0.0

JSON-RPC 2.0 MCP (2025-03-26) server & client with homoiconic tool schemas, resources, prompts, stdio transport, and web/App mounting.

use [ai_mcp]
veko pkg add ai_mcp@1.0.0
Run in Browser PlaygroundDownload 1.0.0.vkpRaw demo.vk

ai_mcp

Model Context Protocol (2025-03-26) JSON-RPC 2.0 server and client for Veko, featuring homoiconic tool schema generation, NDJSON stdio framing, and mountable Streamable HTTP routes over lib/web.

Installation

powershell
veko pkg add ai_mcp@1.0.0

Import in any Veko module:

rebol
use [mcp = ai_mcp]

Architecture Notes

  • Protocol Version 2025-03-26: Implements initialize, notifications/initialized, ping, tools/list, tools/call, resources/list, resources/read, prompts/list, and prompts/get, including JSON-RPC 2.0 batch payloads.
  • Structured JSON-RPC Error Semantics: Protocol violations return standard JSON-RPC error envelopes (-32700 parse error, -32600 invalid request, -32601 method not found, -32602 invalid params, -32603 internal error), whereas recoverable tool runtime err values set isError = true inside the tool result content array per MCP specification.
  • Homoiconic Schema Builder: mcp/tool-schema transforms quoted Veko parameter declarations (quote [ query str 'Search query', limit i64 .optional 'Max rows' ]) directly into JSON Schema { type = 'object', properties = ..., required = ... } maps without stringly-typed boilerplate.
  • Dual Transport Support: Run over line-delimited stdio via mcp/handle-jsonrpc-line or mount directly onto any web/App route via mcp/mount-http (POST JSON-RPC + 202 Accepted notifications + GET SSE endpoint discovery).

API Reference

Constants

  • mcp/MCP-PROTOCOL-VERSION -> '2025-03-26'
  • mcp/ERR-PARSE-ERROR (-32700), mcp/ERR-INVALID-REQUEST (-32600), mcp/ERR-METHOD-NOT-FOUND (-32601), mcp/ERR-INVALID-PARAMS (-32602), mcp/ERR-INTERNAL (-32603)

Types

  • McpServer — Stateful MCP server registry (name, version, instructions, tools, resources, prompts).
  • McpTool — Tool metadata and handler (fn [args map] any).
  • McpResource — Resource URI metadata and reader (fn [uri str] any).
  • McpPrompt / McpPromptArg — Prompt template definition and builder (fn [args map] any).
  • McpClient — Multi-transport MCP client handle.

Server Functions

  • mcp/new-server name version [/instructions instr] -> McpServer
  • mcp/tool-schema spec_blk -> map (JSON Schema object descriptor)
  • mcp/add-tool mut srv name description input_schema handler
  • mcp/add-resource mut srv uri name description mime_type reader
  • mcp/prompt-arg name description required -> McpPromptArg
  • mcp/add-prompt mut srv name description arguments builder
  • mcp/text-content text -> map
  • mcp/prompt-message role text -> map
  • mcp/handle-message mut srv msg -> any (response map, batch block, or .none)
  • mcp/handle-jsonrpc-line mut srv line -> str
  • mcp/mount-http mut app route_path srv

Client Functions

  • mcp/new-http-client endpoint_url [/headers extra_headers] -> McpClient
  • mcp/new-app-client app route_path -> McpClient
  • mcp/new-custom-client endpoint transport_fn -> McpClient
  • mcp/client-initialize mut client -> any
  • mcp/client-ping mut client -> any
  • mcp/client-list-tools mut client -> any
  • mcp/client-call-tool mut client name arguments -> any
  • mcp/client-list-resources mut client -> any
  • mcp/client-read-resource mut client uri -> any
  • mcp/client-list-prompts mut client -> any
  • mcp/client-get-prompt mut client name arguments -> any

Idiomatic Veko Example

rebol
use [mcp = ai_mcp, web]

mut srv = mcp/new-server 'repo-assistant' '1.0.0'

schema = mcp/tool-schema (quote [
    query str 'Search query',
    limit i64 .optional 'Max rows'
])

mcp/add-tool mut srv 'search_docs' 'Search repository documentation' schema fn [args map] any [
    q = str/append '' (map/get args 'query')
    lim = as-i64 (map/get/or args 'limit' [ 5 ])
    `Matched {mold lim} docs for {q}`
]

mut app = web/new-app
mcp/mount-http mut app '/mcp' srv

mut client = mcp/new-app-client app '/mcp'
_ = mcp/client-initialize mut client
res = mcp/client-call-tool mut client 'search_docs' { query = 'concurrency', limit = 3 }
print (json/encode res)
packages/ai_mcp/examples/demo.vkSelf-contained runnable example & assertion suite
Raw .vk
// packages/ai_mcp/examples/demo.vk
// End-to-end verification suite for `ai_mcp` (MCP 2025-03-26 Server & Client).

use [mcp = ai_mcp, web]

assert-eq = fn [label str, got any, want any] [
	if got != want [
		println `[FAIL] {label}: got {mold got}, expected {mold want}`
		return (err `assertion failed: {label}`)
	]
	println `[PASS] {label}`
	.ok
]

assert-true = fn [label str, cond bool] [
	if not cond [
		println `[FAIL] {label}`
		return (err `assertion failed: {label}`)
	]
	println `[PASS] {label}`
	.ok
]

build-demo-server = fn [] mcp/McpServer [
	mut srv = mcp/new-server/instructions 'veko-knowledge-mcp' '1.0.0' 'Search docs, read specs, and draft code reviews.'

	search_schema = mcp/tool-schema (quote [
		query str 'Search query',
		limit i64 .optional 'Max rows'
	])

	mcp/add-tool mut srv 'search_docs' 'Search Veko documentation sections' search_schema fn [args map] any [
		q = str/lower (str/append '' (map/get args 'query'))
		lim = as-i64 (map/get/or args 'limit' [ 5 ])
		if q == 'fail' [
			return (err 'simulated backend timeout')
		]
		`Found {mold lim} matches for "{q}"`
	]

	calc_schema = mcp/tool-schema (quote [
		a i64 'Left operand',
		b i64 'Right operand'
	])

	mcp/add-tool mut srv 'sum_ints' 'Sum two 64-bit integers' calc_schema fn [args map] any [
		a = as-i64 (map/get args 'a')
		b = as-i64 (map/get args 'b')
		mold (a + b)
	]

	mcp/add-resource mut srv 'veko://specs/mcp' 'MCP Spec Summary' 'Protocol notes' 'text/markdown' fn [uri str] any [
		`# Resource {uri}\nTransport: JSON-RPC 2.0 (2025-03-26)`
	]

	mcp/add-prompt mut srv 'code_review' 'Structured Veko code review prompt' [
		(mcp/prompt-arg 'module' 'Target module name' true)
		(mcp/prompt-arg 'focus' 'Optional focus area' false)
	] fn [args map] any [
		mname = str/append '' (map/get args 'module')
		focus = str/append '' (map/get/or args 'focus' [ 'safety and clarity' ])
		[
			(mcp/prompt-message 'user' `Review module {mname} focusing on {focus}.`)
		]
	]

	srv
]

main = fn [] any [
	// 1. Verify homoiconic `tool-schema` generation
	schema = mcp/tool-schema (quote [ query str 'Search query', limit i64 .optional 'Max rows' ])
	_ = assert-eq/or 'tool-schema root type' (map/get schema 'type') 'object' [e -> return e]
	props = map/get schema 'properties'
	qprop = map/get props 'query'
	lprop = map/get props 'limit'
	reqs = map/get schema 'required'
	_ = assert-eq/or 'tool-schema query type' (map/get qprop 'type') 'string' [e -> return e]
	_ = assert-eq/or 'tool-schema limit type' (map/get lprop 'type') 'integer' [e -> return e]
	_ = assert-eq/or 'tool-schema required list' (len reqs) 1 [e -> return e]
	_ = assert-eq/or 'tool-schema required[0]' reqs[0] 'query' [e -> return e]

	// 2. Verify NDJSON stdio line dispatcher (`handle-jsonrpc-line`) & JSON-RPC error codes
	mut srv = build-demo-server

	init_line = mcp/handle-jsonrpc-line mut srv (json/encode {
		jsonrpc = '2.0'
		id      = 1
		method  = 'initialize'
		params  = {
			protocolVersion = '2025-03-26'
			capabilities    = {}
			clientInfo      = { name = 'stdio-tester', version = '1.0.0' }
		}
	})
	init_obj = json/decode init_line
	init_res = map/get init_obj 'result'
	_ = assert-eq/or 'stdio initialize protocolVersion' (map/get init_res 'protocolVersion') '2025-03-26' [e -> return e]

	notif_line = mcp/handle-jsonrpc-line mut srv (json/encode {
		jsonrpc = '2.0'
		method  = 'notifications/initialized'
	})
	_ = assert-eq/or 'stdio notification emits empty line' notif_line '' [e -> return e]

	bad_json_line = mcp/handle-jsonrpc-line mut srv '{broken-json'
	bad_json_obj = json/decode bad_json_line
	_ = assert-eq/or 'JSON-RPC -32700 parse error' (map/get (map/get bad_json_obj 'error') 'code') -32700 [e -> return e]

	no_method_line = mcp/handle-jsonrpc-line mut srv (json/encode {
		jsonrpc = '2.0'
		id      = 99
		method  = 'nonexistent/method'
	})
	no_method_obj = json/decode no_method_line
	_ = assert-eq/or 'JSON-RPC -32601 method not found' (map/get (map/get no_method_obj 'error') 'code') -32601 [e -> return e]

	bad_params_line = mcp/handle-jsonrpc-line mut srv (json/encode {
		jsonrpc = '2.0'
		id      = 100
		method  = 'tools/call'
		params  = { name = 'search_docs', arguments = {} }
	})
	bad_params_obj = json/decode bad_params_line
	_ = assert-eq/or 'JSON-RPC -32602 missing required tool param' (map/get (map/get bad_params_obj 'error') 'code') -32602 [e -> return e]

	// 3. Verify Streamable HTTP route (`mount-http` + `web/handle-request` on `/mcp`) & `McpClient`
	mut app = web/new-app
	mcp/mount-http mut app '/mcp' (build-demo-server)

	// Verify SSE GET discovery on `/mcp`
	sse_get = web/handle-request app (web/Request {
		method  = 'GET'
		path    = '/mcp'
		target  = '/mcp'
		query   = {}
		params  = {}
		headers = {}
		body    = bytes/with-cap 0
	})
	_ = assert-eq/or 'GET /mcp status 200 SSE' sse_get.status 200 [e -> return e]
	_ = assert-eq/or 'GET /mcp Mcp-Protocol-Version header' (map/get/or sse_get.headers 'Mcp-Protocol-Version' [ '' ]) '2025-03-26' [e -> return e]

	// Run full client lifecycle over `/mcp` via `web/handle-request`
	mut client = mcp/new-app-client app '/mcp'
	hs = mcp/client-initialize/or mut client [e -> return e]
	_ = assert-eq/or 'HTTP client initialize version' (map/get hs 'protocolVersion') '2025-03-26' [e -> return e]
	_ = assert-eq/or 'HTTP serverInfo.name' (map/get client.server_info 'name') 'veko-knowledge-mcp' [e -> return e]

	tools = mcp/client-list-tools/or mut client [e -> return e]
	_ = assert-eq/or 'HTTP tools/list count' (len tools) 2 [e -> return e]

	call_ok = mcp/client-call-tool/or mut client 'search_docs' { query = 'Ownership', limit = 3 } [e -> return e]
	_ = assert-eq/or 'HTTP tools/call isError=false' (map/get call_ok 'isError') false [e -> return e]
	c_items = map/get call_ok 'content'
	_ = assert-eq/or 'HTTP tools/call output text' (map/get c_items[0] 'text') 'Found 3 matches for "ownership"' [e -> return e]

	call_sum = mcp/client-call-tool/or mut client 'sum_ints' { a = 19, b = 23 } [e -> return e]
	_ = assert-eq/or 'HTTP tools/call sum_ints' (map/get (map/get call_sum 'content')[0] 'text') '42' [e -> return e]

	call_err = mcp/client-call-tool/or mut client 'search_docs' { query = 'fail' } [e -> return e]
	_ = assert-eq/or 'HTTP tools/call handler err sets isError=true' (map/get call_err 'isError') true [e -> return e]

	resources = mcp/client-list-resources/or mut client [e -> return e]
	_ = assert-eq/or 'HTTP resources/list count' (len resources) 1 [e -> return e]

	rcontents = mcp/client-read-resource/or mut client 'veko://specs/mcp' [e -> return e]
	_ = assert-true/or 'HTTP resources/read text' (str/contains? (map/get rcontents[0] 'text') '2025-03-26') [e -> return e]

	prompts = mcp/client-list-prompts/or mut client [e -> return e]
	_ = assert-eq/or 'HTTP prompts/list count' (len prompts) 1 [e -> return e]

	prompt_res = mcp/client-get-prompt/or mut client 'code_review' { module = 'ai_mcp', focus = 'JSON-RPC edge cases' } [e -> return e]
	pmsgs = map/get prompt_res 'messages'
	_ = assert-eq/or 'HTTP prompts/get message count' (len pmsgs) 1 [e -> return e]
	_ = assert-true/or 'HTTP prompts/get rendered text' (str/contains? (map/get (map/get pmsgs[0] 'content') 'text') 'JSON-RPC edge cases') [e -> return e]

	println 'ai_mcp demo: all checks passed.'
	0
]

main
Cryptographic Provenance

Ed25519 Signature & Merkle Integrity

Registry URL: https://veko-lang.org/pkg/ai_mcp/1.0.0.vkp
Archive SHA-256: a8d05f406b5da2f507a549225777b4bee343fb80eeb0b270309512a74a97bb6d
Merkle Root: 600b309007efd60b4ebfaa3b280e3db4cf327c56e89fd3250c9048f60f606eb1
Signer Trust Key: ed25519:27406074d8681da1d4e6420f5d887cac15c313334c249ac047ba9d4e2df72b70
Embedded VKP1 Manifest

manifest.json

{"name":"ai_mcp","version":"1.0.0","tier":3.0,"tier_label":"Tier 3: AI, Vector RAG & DataFrames","category":"Model Context Protocol","description":"JSON-RPC 2.0 MCP (2025-03-26) server & client with homoiconic tool schemas, resources, prompts, stdio transport, and web/App mounting.","modules":["ai_mcp"],"homepage":"https://veko-lang.org/packages/ai_mcp"}
Bundle Entry PathSizeLeaf SHA-256 Digest
README.md4 KBb2d1910593ec0691b799da1a0610b4c61baf8ee105c0419f81f76b938f6cc784
examples/demo.vk7 KB6067a1211b9ae2903242b9b8b4e61f8a7ade2639dffdfeefd20b1eac4b940779
mod.vk22 KBd52f0e9913fee760a7be476086639e53bc49d0f97aa9d55960308da42bd052f8