Skip to content
Open
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
23 changes: 20 additions & 3 deletions examples/basic-host/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,21 @@ export function hasAppHtml(toolCallInfo: ToolCallInfo): toolCallInfo is Required
}


export function callTool(
/**
* Resolves URI template parameters like {param} with values from structuredContent
*/
function resolveUriParameters(uriTemplate: string, structuredContent: Record<string, unknown>): string {
return uriTemplate.replace(/\{(\w+)\}/g, (match, paramName) => {
const value = structuredContent[paramName];
return value !== undefined ? String(value) : match;
});
}

export async function callTool(
serverInfo: ServerInfo,
name: string,
input: Record<string, unknown>,
): ToolCallInfo {
): Promise<ToolCallInfo> {
log.info("Calling tool", name, "with input", input);
const resultPromise = serverInfo.client.callTool({ name, arguments: input }) as Promise<CallToolResult>;

Expand All @@ -74,10 +84,17 @@ export function callTool(
}

const toolCallInfo: ToolCallInfo = { serverInfo, tool, input, resultPromise };
let result = await resultPromise;

const uiResourceUri = getToolUiResourceUri(tool);
if (uiResourceUri) {
toolCallInfo.appResourcePromise = getUiResource(serverInfo, uiResourceUri);
// Replace URL parameters with values from structuredContent
let resolvedUri = uiResourceUri;
if (result.structuredContent && typeof result.structuredContent === 'object') {
resolvedUri = resolveUriParameters(uiResourceUri, result.structuredContent);
log.info(`Resolved URI: ${uiResourceUri} -> ${resolvedUri}`);
}
toolCallInfo.appResourcePromise = getUiResource(serverInfo, resolvedUri);
}

return toolCallInfo;
Expand Down
6 changes: 3 additions & 3 deletions examples/basic-host/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ function CallToolPanel({ serversPromise, addToolCall, initialServer, initialTool
};

// Submit with optional override for server/tool (used by auto-call)
const handleSubmit = (overrideServer?: ServerInfo, overrideTool?: string) => {
const handleSubmit = async (overrideServer?: ServerInfo, overrideTool?: string) => {
const server = overrideServer ?? selectedServer;
const tool = overrideTool ?? selectedTool;
if (!server) return;

const toolCallInfo = callTool(server, tool, JSON.parse(inputJson));
const toolCallInfo = await callTool(server, tool, JSON.parse(inputJson));
addToolCall(toolCallInfo);

// Update URL for easy refresh/sharing (without triggering navigation)
Expand All @@ -178,7 +178,7 @@ function CallToolPanel({ serversPromise, addToolCall, initialServer, initialTool

return (
<div className={styles.callToolPanel}>
<form onSubmit={(e) => { e.preventDefault(); handleSubmit(); }}>
<form onSubmit={async (e) => { e.preventDefault(); await handleSubmit(); }}>
<label>
Server
<Suspense fallback={<select disabled><option>Loading...</option></select>}>
Expand Down