-
Notifications
You must be signed in to change notification settings - Fork 0
Feature: WebMCP support #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
76de193
WIP: not tested yet
ryan-roemer f31a047
Demo dev console works
ryan-roemer b942cc2
transformer improvements
ryan-roemer a50f685
Remove mcp stuff
ryan-roemer 040b8df
Iframe server and jsdelivr
ryan-roemer ef08892
Update prompt
ryan-roemer 8c66d9f
Abstract
ryan-roemer c6a366d
New iframe transport
ryan-roemer b0c33b8
Fix imports
ryan-roemer bef0cb2
console logging
ryan-roemer e9204fb
Add limit param
ryan-roemer 071ebae
Updates for prod
ryan-roemer 33d7fb5
Update deps
ryan-roemer c40cfc9
Remove early plan
ryan-roemer 160ccbc
Code review feedback and MCP standard updates
ryan-roemer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* global window:false, URLSearchParams:false */ | ||
| import { IframeChildTransport } from "@mcp-b/transports"; | ||
| import { TOOL_SCHEMA, executeSearch } from "./tool-defs.js"; | ||
|
|
||
| const DEBUG = new URLSearchParams(window.location.search).has("debug"); | ||
| const log = (level, ...args) => { | ||
| if (DEBUG) console[level]("[iframe-server]", ...args); // eslint-disable-line no-undef | ||
| }; | ||
|
|
||
| export const initIframeServer = () => { | ||
| if (window === window.parent) return; | ||
|
|
||
| const transport = new IframeChildTransport({ | ||
| allowedOrigins: [ | ||
| "http://localhost:4610", | ||
| "http://127.0.0.1:4610", | ||
| "https://nearform.github.io", | ||
| ], | ||
| }); | ||
|
|
||
| transport.onmessage = async (message) => { | ||
| const { jsonrpc, id, method, params } = message; | ||
| if (jsonrpc !== "2.0" || id == null) return; | ||
|
|
||
| if (method === "tools/list") { | ||
| transport.send({ jsonrpc: "2.0", id, result: { tools: [TOOL_SCHEMA] } }); | ||
| return; | ||
| } | ||
|
|
||
| if (method === "tools/call") { | ||
| try { | ||
| const payload = await executeSearch(params.arguments); | ||
| transport.send({ jsonrpc: "2.0", id, result: payload }); | ||
| } catch (err) { | ||
| transport.send({ | ||
| jsonrpc: "2.0", | ||
| id, | ||
| error: { code: -32000, message: err.message }, | ||
| }); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| transport.send({ | ||
| jsonrpc: "2.0", | ||
| id, | ||
| error: { code: -32601, message: `Method not found: ${method}` }, | ||
| }); | ||
| }; | ||
|
|
||
| transport.onerror = (err) => { | ||
| log("warn", "Transport error:", err); | ||
| }; | ||
|
|
||
| transport.start(); | ||
| log("log", "MCP transport started"); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| 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 = { | ||
| 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.", | ||
| inputSchema: { | ||
| type: "object", | ||
| properties: { | ||
| query: { | ||
| type: "string", | ||
| description: "Search query (e.g. 'microservices architecture')", | ||
| }, | ||
| postType: { | ||
| type: "array", | ||
| items: { type: "string", enum: POST_TYPE_VALUES }, | ||
| description: `Filter by type: ${POST_TYPE_VALUES.join(", ")}. ALMOST NEVER USE THIS. Only set when user literally asks for 'case studies' (work) or 'blog posts' (blog). Omit for all other queries.`, | ||
| }, | ||
| minDate: { | ||
| type: "string", | ||
| description: "Only posts after this date, YYYY-MM-DD (optional)", | ||
| }, | ||
| categoryPrimary: { | ||
| type: "array", | ||
| items: { type: "string", enum: CATEGORIES_LIST }, | ||
| description: `Filter by category: ${CATEGORIES_LIST.join(", ")} (optional)`, | ||
| }, | ||
| maxChunks: { | ||
| type: "number", | ||
| description: "Maximum number of chunks to return (default 50, max 50)", | ||
| }, | ||
| }, | ||
| 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, | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* global navigator:false */ | ||
| import { TOOL_SCHEMA, executeSearch } from "./tool-defs.js"; | ||
|
|
||
| const checkWebMcpSupport = () => { | ||
| if ("modelContext" in navigator) return true; | ||
|
|
||
| const { warn } = console; // eslint-disable-line no-undef | ||
| const BLOG_URL = "https://developer.chrome.com/blog/webmcp-epp"; | ||
| const chromeMatch = navigator.userAgent.match(/Chrome\/(\d+)/); | ||
| const chromeVersion = chromeMatch ? parseInt(chromeMatch[1], 10) : null; | ||
|
|
||
| if (!chromeMatch) { | ||
| warn(`WebMCP requires Chrome 146+. See ${BLOG_URL}`); | ||
| } else if (chromeVersion < 146) { | ||
| warn( | ||
| `WebMCP requires Chrome 146+ (you have ${chromeVersion}). See ${BLOG_URL}`, | ||
| ); | ||
| } else { | ||
| warn( | ||
| `WebMCP not enabled. Go to chrome://flags, search "WebMCP", enable "WebMCP for testing", and relaunch Chrome. See ${BLOG_URL}`, | ||
| ); | ||
| } | ||
| return false; | ||
| }; | ||
|
|
||
| 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) }], | ||
| }; | ||
| }, | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.