# docs.inc.j: the jennifer-docs server's tools, resources and prompt,# shared by docs.j (stdio) and docshttp.j (Streamable HTTP).import"mcp.j"as mcp;
use io;
use os;
use fs;
use json;
use lists;
use regex;
use strings;
defconstMAX_HITSasintinit8;
defconstEXCERPTasintinit72;
defstruct Hit {
page asstring,
count asint,
heading asstring,
line asstring
};
if (len(os.ARGS) != 2ornotfs.isDir(os.ARGS[1])) {
io.eprintf("usage: jennifer run docs.j DOCS_DIR\n");
exit2;
}
def root asstringinitfs.realpath(os.ARGS[1]);
# docPages lists every *.md below the root, relative and sorted.# fs.walk does not follow symlinks below the root.funcdocPages() {
def found aslistofstringinit [];
for (def st infs.walk($root)) {
if (not$st.isDir andstrings.endsWith($st.path, ".md")) {
$found[] = $st.path[len($root) + 1..];
}
}
returnlists.sort($found);
}
def pages aslistofstringinitdocPages();
# requireString reads a string argument with a length cap, or throws.funcrequireString(args asjson.Value, key asstring, max asint) {
if (json.typeOf($args, "/" + $key) != "string") {
throw Error{kind: "value", message: $key + " must be a string"};
}
def v asstringinitstrings.trim(json.asString($args, "/" + $key));
if (len($v) < 2orlen($v) > $max) {
throw Error{kind: "value", message: $key + ": bad length"};
}
return$v;
}
# clip shortens one line of text for an excerpt.funcclip(s asstring) {
def t asstringinitstrings.trim($s);
if (len($t) <= EXCERPT) {
return$t;
}
return$t[..EXCERPT] + "...";
}
# firstHit finds where a page first mentions q: the heading above# that line (not a comment in a code block) and the line itself.funcfirstHit(page asstring, text asstring, q asstring, n asint) {
def heading asstringinit"";
def code asboolinitfalse;
for (def l instrings.split($text, "\n")) {
if (strings.startsWith($l, "```")) {
$code = not$code;
}
if (not$codeandstrings.startsWith($l, "#")) {
$heading = strings.trim($l);
}
if (strings.contains(strings.lower($l), $q)) {
return Hit{page: $page, count: $n, heading: $heading, line: clip($l)};
}
}
return Hit{page: $page, count: $n, heading: "", line: ""};
}
funchitKey(h as Hit) {
return -$h.count;
}
# The tool search_docs: the pages that mention a word or phrase, most# mentions first, each with its section heading and one line.funcsearchDocs(args asjson.Value) {
def q asstringinitstrings.lower(requireString($args, "query", 100));
def hits aslistof Hit init [];
for (def p in$pages) {
def text asstringinitfs.readString($root + "/" + $p);
def n asintinitstrings.count(strings.lower($text), $q);
if ($n > 0) {
$hits[] = firstHit($p, $text, $q, $n);
}
}
if (len($hits) == 0) {
return"No page mentions \"" + $q + "\".";
}
$hits = lists.sortBy($hits, hitKey);
def out aslistofstringinit [];
def shown asintinit0;
for (def h in$hits) {
if ($shown == MAX_HITS) {
break;
}
$out[] = "docs://" + $h.page + " ({$h.count}x) " + $h.heading;
$out[] = " " + $h.line;
$shown = $shown + 1;
}
def total asintinitlen($hits);
$out[] = "matching pages: {$total}; read one with read_doc.";
returnstrings.join($out, "\n");
}
# The tool read_doc: one page, by its path relative to the root. Only# a path from the list docPages made is read; nothing else is opened.funcreadDoc(args asjson.Value) {
def p asstringinitstrings.replace(requireString($args, "path", 200), "docs://", "");
if (notlists.contains($pages, $p)) {
return"No such page: " + $p + ". Use search_docs to find one.";
}
returnfs.readString($root + "/" + $p);
}
# A resource: one page. Only registered URIs reach this handler.funcreadResource(uri asjson.Value) {
def p asstringinitjson.asString($uri, "")[7..];
returnfs.readString($root + "/" + $p);
}
# The prompt explain_function: a ready-made request for the model.funcexplainFunction(args asjson.Value) {
def name asstringinitrequireString($args, "name", 60);
if (notregex.matches("^[a-z][a-zA-Z0-9]*\\.[a-zA-Z][a-zA-Z0-9]*$", $name)) {
throw Error{kind: "value", message: "name: lib.function"};
}
def text asstringinit"Explain the Jennifer function " + $name + ". Use the " +
"search_docs and read_doc tools; give its signature, what it " +
"returns, what it throws and a short example, and name the page.";
def msg asjson.Value initjson.decode('{"role":"user","content":{"type":"text","text":""}}');
$msg = json.set($msg, "/content/text", $text);
returnjson.append(json.list(), "", $msg);
}
funcdocsServer() {
def s asmcp.Server initmcp.server("jennifer-docs", "0.1.0");
def q asjson.Value initmcp.property(
mcp.schema(),
"query",
"string",
"a word or phrase, 2 to 100 characters, such as json.decode",
true);
$s = mcp.addTool(
$s,
"search_docs",
"Search the Jennifer language documentation. Use it before " +
"answering a question about a Jennifer library, module or " +
"function. Returns page paths with a matching line.",
$q,
searchDocs);
def r asjson.Value initmcp.property(
mcp.schema(),
"path",
"string",
"a page path from search_docs, such as libraries/json.md",
true);
$s = mcp.addTool(
$s,
"read_doc",
"Read one page of the Jennifer documentation as Markdown.",
$r,
readDoc);
for (def p in$pages) {
$s = mcp.addResource(
$s,
"docs://" + $p,
$p,
"Jennifer docs: " + $p,
"text/markdown",
readResource);
}
def a asmcp.PromptArg initmcp.promptArg("name", "a function, such as json.decode", true);
$s = mcp.addPrompt(
$s,
"explain_function",
"Explain one Jennifer function from the documentation.",
[$a],
explainFunction);
return$s;
}