当前位置: 首页 > news >正文

[MCP] Advanced

Tool Results

When we call tools, we get the result of the tool call and because we have structured content, we can use it programmatically. This enables rich integrations where the client can understand and act on tool results.
Example:
// Tool returns both human-readable and structured data
const result = await sendMcpMessage('tool',{toolName: 'analyze_code',params: { filePath: 'src/utils/validation.ts' },},{ schema: codeAnalysisSchema },
)// Client can access structured data
const { metrics, suggestions, complexity } = result.structuredContent
if (complexity > 10) {showRefactorWarning(suggestions)
}
updateCodeMetrics(metrics)

With structured content, MCP clients can build sophisticated workflows where tools can trigger automatic actions based on their results, rather than requiring manual user intervention for every step.

 

The structured content is defined by the tool's outputSchema and validated using Zod schemas on the client side, ensuring type safety and reliable data flow between the MCP server and client applications.
Note, keep in mind the example above uses our sendMcpMessage utility function which is really just a wrapper around the postMessage API with the messageId and waiting for the response handled for us.
 
 

Render Data

MCP UI resources can pass initial data directly to iframe components, eliminating the need for additional API calls or complex data fetching logic. This is especially great if the MCP server is running behind authentication and you want to pass private data into the iframe for display. This render data pattern allows the server to provide all necessary context when creating UI resources.

Example:

// Server provides initial data with the UI resource
return {content: [{type: 'resource',resource: {uri: iframeUrl.toString(),mimeType: 'text/html',uiMetadata: {'initial-render-data': { bedInventory },},},},],
}// Client receives data as soon as it emits the `ui-lifecycle-iframe-ready` event
window.addEventListener('message', function handleMessage(event: MessageEvent) {if (event.data?.type !== 'ui-lifecycle-iframe-render-data') returnif (event.data.error) {// event.data.error... handle it} else {const { bedInventory } = event.data.payload// do stuff with this}
})

📜 Passing Render Data to the iframe

 

-----

application:

function DeleteEntryButtonImpl({entry,onDeleted,
}: {entry: { id: number; title: string }onDeleted: () => void
}) {const [isPending, startTransition] = useTransition()const { doubleCheck, getButtonProps } = useDoubleCheck()const { showBoundary } = useErrorBoundary()const handleDelete = () => {startTransition(async () => {try {const result = await sendMcpMessage('tool',{toolName: 'delete_entry',params: { id: entry.id },},{ schema: deleteEntryToolResultSchema },)if (result.structuredContent.success) {onDeleted()} else {showBoundary(new Error('Failed to delete entry'))}} catch (err) {showBoundary(err)}})}return (<button{...getButtonProps({onClick: doubleCheck ? handleDelete : undefined,disabled: isPending,className: `text-sm font-medium px-3 py-1.5 rounded-md border transition-colors ${doubleCheck? 'bg-destructive text-destructive-foreground border-destructive hover:bg-destructive/90': 'text-destructive border-destructive/20 hover:bg-destructive/10 hover:border-destructive/40'} ${isPending ? 'opacity-50 cursor-not-allowed' : ''}`,})}>{isPending ? 'Deleting...' : doubleCheck ? `Confirm?` : 'Delete'}</button>)
}

 

mcp:

	agent.server.registerTool('delete_entry',{title: 'Delete Entry',description: 'Delete a journal entry',annotations: {idempotentHint: true,openWorldHint: false,} satisfies ToolAnnotations,inputSchema: entryIdSchema,outputSchema: { success: z.boolean(), entry: entryWithTagsSchema },},async ({ id }) => {const existingEntry = await agent.db.getEntry(id)invariant(existingEntry, `Entry with ID "${id}" not found`)const confirmed = await elicitConfirmation(agent,`Are you sure you want to delete entry "${existingEntry.title}" (ID: ${id})?`,)if (!confirmed) {const structuredContent = {success: false,entry: existingEntry,}return {structuredContent,content: [createText(`Deleting entry "${existingEntry.title}" (ID: ${id}) rejected by the user.`,),createText(structuredContent),],}}await agent.db.deleteEntry(id)const structuredContent = { success: true, entry: existingEntry }return {structuredContent,content: [createText(`Entry "${existingEntry.title}" (ID: ${id}) deleted successfully`,),createEntryResourceLink(existingEntry),createText(structuredContent),],}},)

 

 

http://www.jsqmd.com/news/387360/

相关文章:

  • C++开发的智能电表读数程序:支持485与计算机读取电量并存功能
  • 2026年自组网设备权威推荐榜:单兵/无人机/车载/船载自组网与抗干扰模块 - 速递信息
  • 光伏mppt扰动观察法并网模型,添加了储能模块,实现了最大功率点的稳定追踪,程序可以正常运行
  • 2026年2月反无人机与低空防御系统权威推荐榜:反无/反反无/频谱感知/融合探测/导航诱骗 - 速递信息
  • iptables 核心动作命令详解
  • GTK4 剪贴板功能实现实战
  • GTK4 拖放功能实现实战
  • 2026年高压电力电缆选购攻略:YJV22厂家实力对比,YTTW防火电缆,高压电力电缆供应厂家哪家靠谱 - 品牌推荐师
  • 2026年杭州优质的GEO优化广告公司口碑推荐,GEO优化/GEO优化AI搜索/GEO服务,GEO优化广告公司找哪家 - 品牌推荐师
  • Reactor 模型全解析 - 教程
  • 不诈骗经济学:有钱为什么他自己不赚,要让给你
  • 2026铝合金高压压铸领域,哪些供货厂家实力出众?精密铝压铸/铝压铸件/铝压铸,铝合金高压压铸产品推荐排行榜 - 品牌推荐师
  • 导师又让重写?AI论文软件 千笔·专业学术智能体 VS 云笔AI,专科生专属神器!
  • Day35仿新浪固定头部
  • 学术论文辅导机构对比:2026年学生反馈分析,考博面试辅导/考博择校指导/申博辅导,学术论文辅导公司推荐排行 - 品牌推荐师
  • C#-使用OpenTK渲染3D模型
  • 毕业论文神器!更贴合继续教育的降AI率网站 千笔·专业降AIGC智能体 VS 灵感ai
  • 用数据说话 10个AI论文写作软件测评:研究生毕业论文+开题报告高效工具推荐
  • 看完就会:AI论文网站 千笔 VS 灵感风暴AI,研究生写作更高效!
  • 发布园子的第一个B站视频!提前祝大家春节快乐!
  • 主流盒马鲜生礼品卡回收方式 - 京顺回收
  • [LangGraph] Command指令
  • 好用还专业!9个降AI率工具测评:本科生必看的降AI率神器
  • 别再瞎找了!10个降AIGC工具测评:自考降AI率全攻略
  • 柑橘叶子病害检测数据集VOC+YOLO格式1102张4类别
  • 07]Delphi10.3中Richedit中的链接可以点击
  • 2026船用减压阀选购指南:这些品牌备受好评,船用减压阀/船用防浪阀/船用疏水阀/船用安全阀,船用减压阀企业推荐 - 品牌推荐师
  • tj
  • 看完就会:10个AI论文软件测评!专科生毕业论文+开题报告神器推荐
  • 研究生必看!全网爆红的AI论文工具 —— 千笔·专业学术智能体