* Centralize example server transport logic in shared utils Move stdio/HTTP transport handling into `server-utils.ts` so example servers only need to call `startServer(createServer)`. Switch HTTP transport to stateless mode and remove legacy SSE endpoints. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Make example servers self-contained with local server-utils Copy `server-utils.ts` into each example's `src/` directory so examples can be used standalone without the shared directory. Updates imports from `"../shared/server-utils.js"` to `"./src/server-utils.js"`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Add `integration-server` example for E2E testing Add dedicated `integration-server` example (duplicated from `basic-server-react`) to preserve E2E testing capabilities while allowing `basic-server-react` to be simplified as a minimal example. Changes: - Add `examples/integration-server` exercising SDK communication APIs - Update E2E tests to use `integration-server` instead of `basic-server-react` and `basic-server-vanillajs` - Use server name labels instead of indices for more robust test selection - Remove `basic-react.png` and `basic-vanillajs.png` golden snapshots 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> * Simplify `basic-server-*` examples - Clarify comments - Return plain text from `get-time` tool instead of JSON - Remove simulated cleanup delay from `onteardown` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
95 lines
2.9 KiB
TypeScript
95 lines
2.9 KiB
TypeScript
/**
|
|
* @file App that demonstrates a few features using MCP Apps SDK with vanilla JS.
|
|
*/
|
|
import { App } from "@modelcontextprotocol/ext-apps";
|
|
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
|
|
import "./global.css";
|
|
import "./mcp-app.css";
|
|
|
|
|
|
const log = {
|
|
info: console.log.bind(console, "[APP]"),
|
|
warn: console.warn.bind(console, "[APP]"),
|
|
error: console.error.bind(console, "[APP]"),
|
|
};
|
|
|
|
|
|
function extractTime(result: CallToolResult): string {
|
|
const { text } = result.content?.find((c) => c.type === "text")!;
|
|
return text;
|
|
}
|
|
|
|
|
|
// Get element references
|
|
const serverTimeEl = document.getElementById("server-time")!;
|
|
const getTimeBtn = document.getElementById("get-time-btn")!;
|
|
const messageText = document.getElementById("message-text") as HTMLTextAreaElement;
|
|
const sendMessageBtn = document.getElementById("send-message-btn")!;
|
|
const logText = document.getElementById("log-text") as HTMLInputElement;
|
|
const sendLogBtn = document.getElementById("send-log-btn")!;
|
|
const linkUrl = document.getElementById("link-url") as HTMLInputElement;
|
|
const openLinkBtn = document.getElementById("open-link-btn")!;
|
|
|
|
|
|
// Create app instance
|
|
const app = new App({ name: "Get Time App", version: "1.0.0" });
|
|
|
|
app.onteardown = async () => {
|
|
log.info("App is being torn down");
|
|
return {};
|
|
};
|
|
|
|
// Register handlers BEFORE connecting
|
|
app.ontoolinput = (params) => {
|
|
log.info("Received tool call input:", params);
|
|
};
|
|
|
|
app.ontoolresult = (result) => {
|
|
log.info("Received tool call result:", result);
|
|
serverTimeEl.textContent = extractTime(result);
|
|
};
|
|
|
|
app.onerror = log.error;
|
|
|
|
|
|
// Add event listeners
|
|
getTimeBtn.addEventListener("click", async () => {
|
|
try {
|
|
log.info("Calling get-time tool...");
|
|
const result = await app.callServerTool({ name: "get-time", arguments: {} });
|
|
log.info("get-time result:", result);
|
|
serverTimeEl.textContent = extractTime(result);
|
|
} catch (e) {
|
|
log.error(e);
|
|
serverTimeEl.textContent = "[ERROR]";
|
|
}
|
|
});
|
|
|
|
sendMessageBtn.addEventListener("click", async () => {
|
|
const signal = AbortSignal.timeout(5000);
|
|
try {
|
|
log.info("Sending message text to Host:", messageText.value);
|
|
const { isError } = await app.sendMessage(
|
|
{ role: "user", content: [{ type: "text", text: messageText.value }] },
|
|
{ signal },
|
|
);
|
|
log.info("Message", isError ? "rejected" : "accepted");
|
|
} catch (e) {
|
|
log.error("Message send error:", signal.aborted ? "timed out" : e);
|
|
}
|
|
});
|
|
|
|
sendLogBtn.addEventListener("click", async () => {
|
|
log.info("Sending log text to Host:", logText.value);
|
|
await app.sendLog({ level: "info", data: logText.value });
|
|
});
|
|
|
|
openLinkBtn.addEventListener("click", async () => {
|
|
log.info("Sending open link request to Host:", linkUrl.value);
|
|
const { isError } = await app.openLink({ url: linkUrl.value });
|
|
log.info("Open link request", isError ? "rejected" : "accepted");
|
|
});
|
|
|
|
|
|
// Connect to host
|
|
app.connect();
|