网站首页 > 技术文章 正文
大家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发!
什么是 use-mcp
use-mcp 是一个轻量级的 React Hook,用于连接模型上下文协议 (MCP) 服务器,简化了实现 MCP 标准的 AI 系统的身份验证和工具调用。
典型特征包括:
- 自动连接管理,支持重新连接和重试
- OAuth 身份验证流程处理,支持弹出窗口和回退
- 简单的 React hook 接口,用于 MCP 集成
- TypeScript 类型,用于编辑器辅助和类型检查
- 全面的日志记录,方便调试
- 支持 HTTP 和 SSE(服务器发送事件)传输
目前 use-mcp 在 Github 通过 MIT 协议开源,短短几周已经有超过 0.6k 的 star,是一个值得关注的前端开源项目。
如何使用 use-mcp
首先需要安装相应的依赖:
npm install use-mcp
# or
pnpm add use-mcp
# or
yarn add use-mcp
接着从 use-mcp/react 导入相应的 useMcp Hooks :
import {useMcp} from 'use-mcp/react'
function MyAIComponent() {
const {
state,
// 连接状态: 'discovering' | 'authenticating' | 'connecting' | 'loading' | 'ready' | 'failed'
tools,
// MCP 服务器提供的可用工具
error,
// 连接错误的消息
callTool,
// 调用 MCP 服务器上的工具函数
retry,
// 手动连接重试
authenticate,
// 手动触发认证
clearStorage,
// 清除存储的令牌和凭证
} = useMcp({
url: 'https://your-mcp-server.com',
clientName: 'My App',
autoReconnect: true,
})
// 处理不同的状态
if (state === 'failed') {
return (
<div>
<p>Connection failed: {error}</p>
<button onClick={retry}>Retry</button>
<button onClick={authenticate}>Authenticate Manually</button>
</div>
)
}
if (state !== 'ready') {
return <div>Connecting to AI service...</div>
}
// 可用的工具
const handleSearch = async () => {
try {
const result = await callTool('search', { query: 'example search'})
console.log('Search results:', result)
} catch (err) {
console.error('Tool call failed:', err)
}
}
return (
<div>
<h2>Available Tools: {tools.length}</h2>
<ul>
{tools.map(tool => (
<li key={tool.name}>{tool.name}</li>
))}
</ul>
<button onClick={handleSearch}>Search</button>
</div>
)
}
如果要处理 OAuth 身份验证流程,开发者需要在应用中设置回调端点,下面是使用 React Router 的典型示例:
// App.tsx with React Router
import {BrowserRouter as Router, Routes, Route} from 'react-router-dom'
import {useEffect} from 'react'
import {onMcpAuthorization} from 'use-mcp'
function OAuthCallback() {
useEffect(() => {
onMcpAuthorization()
}, [])
return (
<div>
<h1>Authenticating...</h1>
<p>This window should close automatically.</p>
</div>
)
}
function App() {
return (
<Router>
<Routes>
<Route path="/oauth/callback" element={<OAuthCallback />} />
<Route path="/" element={<YourMainComponent />} />
</Routes>
</Router>
)
}
下面是使用 Next.js Pages Router:
// pages/oauth/callback.tsx
import {useEffect} from 'react'
import {onMcpAuthorization} from 'use-mcp'
export default function OAuthCallbackPage() {
useEffect(() => {
onMcpAuthorization()
}, [])
return (
<div>
<h1>Authenticating...</h1>
<p>This window should close automatically.</p>
</div>
)
}
更多关于 use-mcp 的示例可以参考文末资料,本文不再过多展开。
参考资料
https://github.com/modelcontextprotocol/use-mcp
https://dev.to/michael_watson_8780e10ad7/model-context-protocol-mcp-is-the-react-moment-for-ai-3nhg
https://dev.to/noah-00/modern-ui-development-with-ai-coding-figma-designs-via-cursor-mcp-server-155m
https://www.latent.space/p/why-mcp-won
猜你喜欢
- 2025-08-05 智能图书馆管理系统开发实战系列(二):高保真原型设计
- 2025-08-05 Next.js 14 Server Actions:告别API路由的全栈开发新范式
- 2025-08-05 我如何驯服 Cursor AI,让它每次都生成正确代码
- 2025-08-05 React中实现苹果的Liquid Glass新拟态UI
- 2025-08-05 前端大一统时代来了
- 2025-08-05 基于 Rust 和 React 新一代全栈 Web 框架 Tuono 强势来袭!
- 2025-08-05 AI 编程的三步走:从“能用”到“能优化”
- 2025-08-05 从Rax+DX到React,一次跨端组件重写的AI提效探索
- 2025-05-22 如何通过 OpenMemory MCP 让你的客户端更具上下文感知能力
- 2025-05-22 你在 Next.js 中用错 "use client" 了吗?
- 1521℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 640℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 527℃MySQL service启动脚本浅析(r12笔记第59天)
- 492℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 492℃启用MySQL查询缓存(mysql8.0查询缓存)
- 479℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 461℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 459℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- windowsscripthost (69)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)