Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions public/app/data/iframe-server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global window:false, URLSearchParams:false */
/* global window:false, navigator:false, URLSearchParams:false */
import { IframeChildTransport } from "@mcp-b/transports";
import { TOOL_SCHEMA, executeSearch } from "./tool-defs.js";
import { TOOLS } from "./tools/index.js";

const DEBUG = new URLSearchParams(window.location.search).has("debug");
const log = (level, ...args) => {
Expand All @@ -23,23 +23,43 @@ export const initIframeServer = () => {
if (jsonrpc !== "2.0" || id == null) return;

if (method === "tools/list") {
transport.send({ jsonrpc: "2.0", id, result: { tools: [TOOL_SCHEMA] } });
const tools = TOOLS.map((tool) => {
const { execute, ...rest } = tool; // eslint-disable-line no-unused-vars
return rest;
});
transport.send({
jsonrpc: "2.0",
id,
result: { tools },
});
return;
}

if (method === "tools/call") {
try {
// MCP tools/call result must contain a `content` array and optional `isError` flag.
// See: https://modelcontextprotocol.io/specification/2025-06-18/server/tools
// Format: { content: [{ type: "text", text: "..." }], isError: false }
const payload = await executeSearch(params.arguments);
const tool = TOOLS.find((t) => t.name === params.name);
if (!tool) {
transport.send({
jsonrpc: "2.0",
id,
error: {
code: -32602,
message: `Unknown tool: ${params.name}`,
},
});
return;
}
// Invoke with `modelContextTesting` to gut check tooling invocation.
// Could call directly if we wanted, but checks for future compliance.
const result = await navigator.modelContextTesting.executeTool(
tool.name,
JSON.stringify(params.arguments ?? {}),
);
Comment thread
ryan-roemer marked this conversation as resolved.
const parsedResult = JSON.parse(result);
transport.send({
jsonrpc: "2.0",
id,
result: {
content: [{ type: "text", text: JSON.stringify(payload, null, 2) }],
isError: false,
},
result: { isError: false, ...parsedResult },
});
} catch (err) {
transport.send({
Expand Down
3 changes: 3 additions & 0 deletions public/app/data/tools/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { searchTool } from "./search.js";

export const TOOLS = [searchTool];
69 changes: 36 additions & 33 deletions public/app/data/tool-defs.js → public/app/data/tools/search.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { searchPosts } from "./search.js";
import { POST_TYPE_OPTIONS } from "../components/forms.js";
import { CATEGORIES_LIST } from "../components/category.js";
import { searchPosts } from "../search.js";
import { POST_TYPE_OPTIONS } from "../../components/forms.js";
import { CATEGORIES_LIST } from "../../components/category.js";

const POST_TYPE_VALUES = POST_TYPE_OPTIONS.map((o) => o.value);

export const TOOL_SCHEMA = {
export const searchTool = {
name: "search_nearform_knowledge",
description:
"Vector search across Nearform blog posts and case studies using semantic similarity. Returns matching posts with titles, URLs, dates, and similarity scores.",
Expand Down Expand Up @@ -36,33 +36,36 @@ export const TOOL_SCHEMA = {
},
required: ["query"],
},
};

export const executeSearch = async (args) => {
const result = await searchPosts({
query: args.query,
postType: args.postType || [],
minDate: args.minDate || "",
categoryPrimary: args.categoryPrimary || [],
chunkSize: 256,
maxChunks: args.maxChunks,
});
return {
postCount: result.posts.length,
posts: result.posts.map((p) => ({
slug: p.slug,
title: p.title,
href: p.href,
date: p.date,
type: p.postType,
categories: p.categories,
similarity: p.similarityMax,
})),
chunks: result.chunks.map((c) => ({
slug: c.slug,
text: c.text,
similarity: c.similarity,
})),
metadata: result.metadata,
};
annotations: { readOnlyHint: true },
execute: async (args) => {
const result = await searchPosts({
query: args.query,
postType: args.postType || [],
minDate: args.minDate || "",
categoryPrimary: args.categoryPrimary || [],
chunkSize: 256,
maxChunks: args.maxChunks,
});
const payload = {
postCount: result.posts.length,
posts: result.posts.map((p) => ({
slug: p.slug,
title: p.title,
href: p.href,
date: p.date,
type: p.postType,
categories: p.categories,
similarity: p.similarityMax,
})),
chunks: result.chunks.map((c) => ({
slug: c.slug,
text: c.text,
similarity: c.similarity,
})),
metadata: result.metadata,
};
return {
content: [{ type: "text", text: JSON.stringify(payload, null, 2) }],
};
},
};
15 changes: 4 additions & 11 deletions public/app/data/webmcp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global navigator:false */
import { TOOL_SCHEMA, executeSearch } from "./tool-defs.js";
import { TOOLS } from "./tools/index.js";

const checkWebMcpSupport = () => {
if ("modelContext" in navigator) return true;
Expand All @@ -26,14 +26,7 @@ const checkWebMcpSupport = () => {
export const registerWebMcpTools = () => {
if (!checkWebMcpSupport()) return;

navigator.modelContext.registerTool({
...TOOL_SCHEMA,
annotations: { readOnlyHint: true },
execute: async (input) => {
const payload = await executeSearch(input);
return {
content: [{ type: "text", text: JSON.stringify(payload, null, 2) }],
};
},
});
for (const tool of TOOLS) {
navigator.modelContext.registerTool(tool);
}
};
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
import { initData } from "./app/data/index.js";
import { initializeWebMCPPolyfill } from "@mcp-b/webmcp-polyfill";

initializeWebMCPPolyfill();
initializeWebMCPPolyfill({ installTestingShim: true });

// Preload data and model in parallel (fire and forget)
// These cache their results, so first search will be fast
Expand Down