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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function MyAIComponent() {
retry, // Retry connection manually
authenticate, // Manually trigger authentication
clearStorage, // Clear stored tokens and credentials
authProvider, // Class for handling OAuth authentication and token management
} = useMcp({
url: 'https://your-mcp-server.com',
clientName: 'My App',
Expand Down Expand Up @@ -115,7 +116,7 @@ function MyAIComponent() {
))}
</ul>
<button onClick={handleSearch}>Search</button>

{/* Example: Display and read resources */}
{resources.length > 0 && (
<div>
Expand All @@ -128,7 +129,7 @@ function MyAIComponent() {
</button>
</div>
)}

{/* Example: Use prompts */}
{prompts.length > 0 && (
<div>
Expand Down Expand Up @@ -249,7 +250,8 @@ function useMcp(options: UseMcpOptions): UseMcpResult
| `disconnect` | `() => void` | Disconnect from the MCP server |
| `authenticate` | `() => void` | Manually trigger authentication |
| `clearStorage` | `() => void` | Clear all stored authentication data |
| `authProvider` | `BrowserOAuthClientProvider` | Class for handling OAuth authentication and token management |

## License

MIT
MIT
3 changes: 2 additions & 1 deletion src/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function MyChatComponent() {
{mcp.state === 'authenticating' && <p>Waiting for authentication...</p>}
{mcp.state === 'ready' && (
<p>
Connected! Tools: {mcp.tools.length}, Resources: {mcp.resources.length + mcp.resourceTemplates.length},
Connected! Tools: {mcp.tools.length}, Resources: {mcp.resources.length + mcp.resourceTemplates.length},
Prompts: {mcp.prompts.length}
</p>
)}
Expand Down Expand Up @@ -230,6 +230,7 @@ export default MyChatComponent
- disconnect(): Disconnects the client from the server.
- authenticate(): Manually attempts to start the authentication flow. Useful for triggering the popup via a user click if it was initially blocked.
- clearStorage(): Removes all authentication-related data (tokens, client info, code verifier, state) for the configured server URL from localStorage. Useful for development or allowing users to "log out". Automatically disconnects the client.
- authProvider: An internal class that manages OAuth authentication flows. This handles all OAuth-related operations behind the scenes and is automatically used by the hook (you typically won't need to interact with it directly).

### Setting up the OAuth Callback Route

Expand Down
3 changes: 3 additions & 0 deletions src/react/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Tool, Resource, ResourceTemplate, Prompt } from '@modelcontextprotocol/sdk/types.js'
import { BrowserOAuthClientProvider } from '../auth/browser-provider'

export type UseMcpOptions = {
/** The /sse URL of your remote MCP server */
Expand Down Expand Up @@ -112,4 +113,6 @@ export type UseMcpResult = {
authenticate: () => void
/** Clears all stored authentication data (tokens, client info, etc.) for this server URL from localStorage. */
clearStorage: () => void
/** The OAuth client provider used for authentication */
authProvider: BrowserOAuthClientProvider | null
}
1 change: 1 addition & 0 deletions src/react/useMcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,5 +839,6 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
disconnect,
authenticate,
clearStorage,
authProvider: authProviderRef.current,
}
}