Token导航 LogoToken导航TokenDH.com
use MCP logo
AI代理未说明官方级别未说明来源级核验

use MCP

MCP Server

一个轻量级的React钩子,用于连接Model Context Protocol(MCP)服务器,简化AI系统的认证和工具调用。

工具数

0

提示词数

0

GitHub Stars

0

资源数

0
AI代理TypeScriptOAuth认证本地部署

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

Automata-Labs-team

提供方

Automata-Labs-team

最后核验

2026/5/17 20:19

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

详细介绍

🦑 使用MCP(可能是指某种工具、框架或协议)🦑

](https://github.com/modelcontextprotocol/use-mcp)  ](https://www.npmjs.com/package/use-mcp)

一个用于连接的轻量级React Hook 模型上下文协议(MCP) 服务器。简化了实施MCP标准的AI系统的身份验证和工具调用。

试试看: 聊天演示 | MCP 检查器 | Cloudflare Workers AI 实验室

安装

npm install use-mcp
# or
pnpm add use-mcp
# or
yarn add use-mcp

发展

要运行包含所有示例和服务器的开发环境:

pnpm dev

这开始了:

  • 检查员http://localhost:5001 - MCP服务器调试工具
  • 聊天用户界面http://localhost:5002 - 示例聊天界面
  • Hono MCP 服务器http://localhost:5101 - 示例MCP服务器
  • CF代理MCP服务器http://localhost:5102 - Cloudflare Workers AI MCP 服务器

测试

集成测试位于 test/ 默认情况下,以无头模式运行目录(或:默认情况下,以无头模式执行目录中的操作)

cd test && pnpm test              # Run tests headlessly (default)
cd test && pnpm test:headed       # Run tests with visible browser
cd test && pnpm test:watch        # Run tests in watch mode
cd test && pnpm test:ui           # Run tests with interactive UI

特点/特性

  • 🔄 自动连接管理,支持重新连接和重试
  • 🔐 支持弹出窗口和回退机制的OAuth认证流程处理
  • 📦 简单的React钩子接口,用于MCP集成
  • 全面支持MCP工具、资源和提示
  • 📄 访问服务器资源并读取其内容
  • 💬 使用服务器提供的提示模板
  • 🧰 用于编辑器辅助和类型检查的TypeScript类型
  • 📝 全面的日志记录用于调试
  • 🌐 支持HTTP和SSE(服务器发送事件)两种传输方式
  • 🧠 服务器驱动的引出支持,支持可选的处理器注册

快速入门

import { useEffect } from 'react'
import { useMcp } from 'use-mcp/react'

function MyAIComponent() {
  const {
    state,          // Connection state: 'discovering' | 'pending_auth' | 'authenticating' | 'connecting' | 'loading' | 'ready' | 'failed'
    tools,          // Available tools from MCP server
    resources,      // Available resources from MCP server
    prompts,        // Available prompts from MCP server
    error,          // Error message if connection failed
    callTool,       // Function to call tools on the MCP server
    readResource,   // Function to read resource contents
    getPrompt,      // Function to get prompt messages
    retry,          // Retry connection manually
    authenticate,   // Manually trigger authentication
    clearStorage,   // Clear stored tokens and credentials
    onEliciation,   // Register handler for server elicitations
  } = useMcp({
    url: 'https://your-mcp-server.com',
    clientName: 'My App',
    autoReconnect: true,
  })

  useEffect(() => {
    if (state !== 'ready') return
    onEliciation(async params => {
      if (params.prompt === 'confirm-action') {
        return { action: 'respond', content: { type: 'text', text: 'Approved' } }
      }
      return { action: 'decline' }
    })
  }, [state, onEliciation])

  // Handle different states
  if (state === 'failed') {
    return (
      

        
Connection failed: {error}

        Retry
        Authenticate Manually
      

    )
  }

  if (state !== 'ready') {
    return 
Connecting to AI service...

  }

  // Use available tools
  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 (
    

      
Available Tools: {tools.length}

      

        {tools.map(tool => (
          
{tool.name}

        ))}
      

      Search
      
      {/* Example: Display and read resources */}
      {resources.length > 0 && (
        

          
Resources: {resources.length}

           {
            const content = await readResource(resources[0].uri)
            console.log('Resource content:', content)
          }}>
            Read First Resource
          
        

      )}
      
      {/* Example: Use prompts */}
      {prompts.length > 0 && (
        

          
Prompts: {prompts.length}

           {
            const result = await getPrompt(prompts[0].name)
            console.log('Prompt messages:', result.messages)
          }}>
            Get First Prompt
          
        

      )}
    

  )
}

设置OAuth回调

为了处理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 (
    

      
Authenticating...

      
This window should close automatically.

    

  )
}

function App() {
  return (
    
      
        } />
        } />
      
    
  )
}

使用 Next.js Pages 路由器

// pages/oauth/callback.tsx
import { useEffect } from 'react'
import { onMcpAuthorization } from 'use-mcp'

export default function OAuthCallbackPage() {
  useEffect(() => {
    onMcpAuthorization()
  }, [])

  return (
    

      
Authenticating...

      
This window should close automatically.

    

  )
}

API 参考文档

useMcp 钩子

function useMcp(options: UseMcpOptions): UseMcpResult

选项

选项类型描述
urlstring必需的您的MCP服务器的URL
clientNamestringOAuth注册时您的客户名称
clientUristringOAuth 注册时客户端的 URI
callbackUrlstringOAuth重定向的自定义回调URL(默认为 /oauth/callback (基于当前的起源)
storageKeyPrefixstring用于在localStorage中存储OAuth数据的键前缀(默认为“mcp:auth”)
clientConfigobjectMCP客户端身份的自定义配置
debugboolean是否启用详细调试日志记录
autoRetry`boolean \number`如果初始连接失败,则自动重试连接,延迟以毫秒为单位
autoReconnect`boolean \number`如果已建立的连接丢失,则自动重新连接,延迟以毫秒为单位(默认:3000)
transportType`'auto' \'http' \'sse'`传输类型偏好:'auto'(HTTP,备选SSE),'http'(仅HTTP),'sse'(仅SSE)(默认:'auto')
preventAutoAuthboolean防止初始连接时自动弹出身份验证窗口(默认:false)
onPopupWindow`(url: string, features: string, window: Window \null) => void`认证弹窗打开后立即调用回调函数

返回值

属性类型描述
statestring 当前连接状态:'发现中', '等待授权', '正在授权', '正在连接', '加载中', '就绪', '失败'
toolsTool[]来自MCP服务器的可用工具
resourcesResource[]来自MCP服务器的可用资源
resourceTemplatesResourceTemplate[]来自MCP服务器的可用资源模板
promptsPrompt[]来自MCP服务器的可用提示
error`string \undefined`如果连接失败,显示错误信息
authUrl`string \undefined`如果弹窗被阻止,则使用手动认证URL
log`{ level: 'debug' \'info' \'warn' \'error'; message: string; timestamp: number }[]`日志消息数组
callTool(name: string, args?: Record) => Promise在MCP服务器上调用工具的功能
listResources() => Promise 刷新可用资源列表
readResource(uri: string) => Promise }>读取特定资源的内容
listPrompts() => Promise刷新可用提示列表
getPrompt(name: string, args?: Record) => Promise }>获取带有可选参数的特定提示
retry() => void手动尝试重新连接
disconnect() => void与MCP服务器断开连接
authenticate() => void手动触发身份验证
clearStorage() => void 清除所有存储的认证数据
onEliciation`(cb: (params) => Promise \ElicitResult) => void`注册一个处理程序以响应服务器的引出请求

许可证

麻省理工学院(MIT)

目录标签

目录标签

AI代理TypeScriptOAuth认证本地部署React钩子MCP连接AI集成工具调用

接入字段

传输方式(transport,传输协议)

未说明

鉴权方式(authType,认证方式)

oauth

工具数量(toolCount,工具数)

0

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

未说明oauth部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

仍需确认:installCommand

来源信息

继续浏览同类 MCP