@mcp-fe/react-tools 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +203 -0
- package/README.md +102 -0
- package/context/MCPToolsContext.d.ts +126 -0
- package/context/MCPToolsContext.d.ts.map +1 -0
- package/hooks/useMCPTool.d.ts +123 -0
- package/hooks/useMCPTool.d.ts.map +1 -0
- package/hooks/useMCPToolHelpers.d.ts +45 -0
- package/hooks/useMCPToolHelpers.d.ts.map +1 -0
- package/index.d.ts +21 -0
- package/index.d.ts.map +1 -0
- package/index.mjs +2354 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity granting the License.
|
|
13
|
+
|
|
14
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
15
|
+
other entities that control, are controlled by, or are under common
|
|
16
|
+
control with that entity. For the purposes of this definition,
|
|
17
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
18
|
+
direction or management of such entity, whether by contract or
|
|
19
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
23
|
+
exercising permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" shall mean the preferred form for making modifications,
|
|
26
|
+
including but not limited to software source code, documentation
|
|
27
|
+
source, and configuration files.
|
|
28
|
+
|
|
29
|
+
"Object" shall mean any form resulting from mechanical
|
|
30
|
+
transformation or translation of a Source form, including but
|
|
31
|
+
not limited to compiled object code, generated documentation,
|
|
32
|
+
and conversions to other media types.
|
|
33
|
+
|
|
34
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
35
|
+
Object form, made available under the License, as indicated by a
|
|
36
|
+
copyright notice that is included in or attached to the work
|
|
37
|
+
(which shall not include communications that are solely about
|
|
38
|
+
and bear an appropriate legal notice).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based upon (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and derivative works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control
|
|
57
|
+
systems, and issue tracking systems that are managed by, or on behalf
|
|
58
|
+
of, the Licensor for the purpose of discussing and improving the Work,
|
|
59
|
+
but excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to use, reproduce, modify, merge, publish,
|
|
70
|
+
distribute, sublicense, and/or sell copies of the Work, and to
|
|
71
|
+
permit persons to whom the Work is furnished to do so, subject to
|
|
72
|
+
the following conditions:
|
|
73
|
+
|
|
74
|
+
The above copyright notice and this permission notice shall be
|
|
75
|
+
included in all copies or substantial portions of the Work.
|
|
76
|
+
|
|
77
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
78
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
79
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
80
|
+
(except as stated in this section) patent license to make, have made,
|
|
81
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
82
|
+
where such license applies only to those patent claims licensable
|
|
83
|
+
by such Contributor that are necessarily infringed by their
|
|
84
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
85
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
86
|
+
institute patent litigation against any entity (including a
|
|
87
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
88
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
89
|
+
or contributory patent infringement, then any patent licenses
|
|
90
|
+
granted to You under this License for that Work shall terminate
|
|
91
|
+
as of the date such litigation is filed.
|
|
92
|
+
|
|
93
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
94
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
95
|
+
modifications, and in Source or Object form, provided that You
|
|
96
|
+
meet the following conditions:
|
|
97
|
+
|
|
98
|
+
(a) You must give any other recipients of the Work or
|
|
99
|
+
Derivative Works a copy of this License; and
|
|
100
|
+
|
|
101
|
+
(b) You must cause any modified files to carry prominent notices
|
|
102
|
+
stating that You changed the files; and
|
|
103
|
+
|
|
104
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
105
|
+
that You distribute, all copyright, patent, trademark, and
|
|
106
|
+
attribution notices from the Source form of the Work,
|
|
107
|
+
excluding those notices that do not pertain to any part of
|
|
108
|
+
the Derivative Works; and
|
|
109
|
+
|
|
110
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
111
|
+
distribution, then any Derivative Works that You distribute must
|
|
112
|
+
include a readable copy of the attribution notices contained
|
|
113
|
+
within such NOTICE file, excluding those notices that do not
|
|
114
|
+
pertain to any part of the Derivative Works, in at least one
|
|
115
|
+
of the following places: within a NOTICE text file distributed
|
|
116
|
+
as part of the Derivative Works; within the Source form or
|
|
117
|
+
documentation, if provided along with the Derivative Works; or,
|
|
118
|
+
within a display generated by the Derivative Works, if and
|
|
119
|
+
wherever such third-party notices normally appear. The contents
|
|
120
|
+
of the NOTICE file are for informational purposes only and
|
|
121
|
+
do not modify the License. You may add Your own attribution
|
|
122
|
+
notices within Derivative Works that You distribute, alongside
|
|
123
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
124
|
+
that such additional attribution notices cannot be construed
|
|
125
|
+
as modifying the License.
|
|
126
|
+
|
|
127
|
+
You may add Your own copyright notice to Your modifications and
|
|
128
|
+
may provide additional or different license terms and conditions
|
|
129
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
130
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
131
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
132
|
+
the conditions stated in this License.
|
|
133
|
+
|
|
134
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
135
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
136
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
137
|
+
this License, without any additional terms or conditions.
|
|
138
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
139
|
+
the terms of any separate license agreement you may have executed
|
|
140
|
+
with Licensor regarding such Contributions.
|
|
141
|
+
|
|
142
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
143
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
144
|
+
except as required for reasonable and customary use in describing the
|
|
145
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
146
|
+
|
|
147
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
148
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
149
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
150
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
151
|
+
implied, including, without limitation, any warranties or conditions
|
|
152
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
153
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
154
|
+
appropriateness of using or redistributing the Work and assume any
|
|
155
|
+
risks associated with Your exercise of permissions under this License.
|
|
156
|
+
|
|
157
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
158
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
159
|
+
unless required by applicable law (such as deliberate and grossly
|
|
160
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
161
|
+
liable to You for damages, including any direct, indirect, special,
|
|
162
|
+
incidental, or consequential damages of any character arising as a
|
|
163
|
+
result of this License or out of the use or inability to use the
|
|
164
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
165
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
166
|
+
other commercial damages or losses), even if such Contributor
|
|
167
|
+
has been advised of the possibility of such damages.
|
|
168
|
+
|
|
169
|
+
9. Accepting Warranty or Support. You may choose to offer, and to
|
|
170
|
+
charge a fee for, warranty, support, indemnity or other liability
|
|
171
|
+
obligations and/or rights consistent with this License. However, in
|
|
172
|
+
accepting such obligations, You may act only on Your own behalf and on
|
|
173
|
+
Your sole responsibility, not on behalf of any other Contributor, and
|
|
174
|
+
only if You agree to indemnify, defend, and hold each Contributor
|
|
175
|
+
harmless for any liability incurred by, or claims asserted against,
|
|
176
|
+
such Contributor by reason of your accepting any such warranty or support.
|
|
177
|
+
|
|
178
|
+
END OF TERMS AND CONDITIONS
|
|
179
|
+
|
|
180
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
181
|
+
|
|
182
|
+
To apply the Apache License to your work, attach the following
|
|
183
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
184
|
+
replaced with your own identifying information. (Don't include
|
|
185
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
186
|
+
comment syntax for the file format. We also recommend that a
|
|
187
|
+
file or class name and description of purpose be included on the
|
|
188
|
+
same page as the copyright notice for easier identification within
|
|
189
|
+
third-party archives.
|
|
190
|
+
|
|
191
|
+
Copyright 2026 Michal Kopecky
|
|
192
|
+
|
|
193
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
194
|
+
you may not use this file except in compliance with the License.
|
|
195
|
+
You may obtain a copy of the License at
|
|
196
|
+
|
|
197
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
198
|
+
|
|
199
|
+
Unless required by applicable law or agreed to in writing, software
|
|
200
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
201
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
202
|
+
See the License for the specific language governing permissions and
|
|
203
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# @mcp-fe/react-tools
|
|
2
|
+
|
|
3
|
+
> React hooks and components for seamless integration with MCP (Model Context Protocol) tools.
|
|
4
|
+
|
|
5
|
+
Simplify dynamic MCP tool integration in React applications with automatic lifecycle management, reference counting, and full access to React features.
|
|
6
|
+
|
|
7
|
+
## ✨ Key Features
|
|
8
|
+
|
|
9
|
+
- 🔄 **Automatic Lifecycle** - Register on mount, unregister on unmount
|
|
10
|
+
- 📊 **Reference Counting** - Share tools across multiple components safely
|
|
11
|
+
- ⚡ **Re-render Safe** - Smart refs prevent duplicate registrations
|
|
12
|
+
- 🎯 **Full React Access** - Handlers have complete access to state, props, and context
|
|
13
|
+
- 🔌 **Optional Context** - Works standalone or with Provider
|
|
14
|
+
- 📘 **TypeScript First** - Complete type safety out of the box
|
|
15
|
+
|
|
16
|
+
## 📦 Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @mcp-fe/react-tools
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm add @mcp-fe/react-tools
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
yarn add @mcp-fe/react-tools
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## 🚀 Quick Example
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
import { useMCPTool } from '@mcp-fe/react-tools';
|
|
34
|
+
|
|
35
|
+
function MyComponent() {
|
|
36
|
+
const user = useUser();
|
|
37
|
+
|
|
38
|
+
useMCPTool({
|
|
39
|
+
name: 'get_user_profile',
|
|
40
|
+
description: 'Get current user profile',
|
|
41
|
+
inputSchema: {
|
|
42
|
+
type: 'object',
|
|
43
|
+
properties: {}
|
|
44
|
+
},
|
|
45
|
+
handler: async () => ({
|
|
46
|
+
content: [{
|
|
47
|
+
type: 'text',
|
|
48
|
+
text: JSON.stringify({
|
|
49
|
+
userId: user.id,
|
|
50
|
+
name: user.name,
|
|
51
|
+
email: user.email
|
|
52
|
+
})
|
|
53
|
+
}]
|
|
54
|
+
})
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return <div>Profile tool is active!</div>;
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 📚 Documentation
|
|
62
|
+
|
|
63
|
+
**[📖 View Complete Documentation →](./docs/index.md)**
|
|
64
|
+
|
|
65
|
+
Comprehensive documentation is available in the [`docs/`](./docs/) directory:
|
|
66
|
+
|
|
67
|
+
- **[Getting Started](./docs/getting-started.md)** - Installation, quick start, and basic usage
|
|
68
|
+
- **[API Reference](./docs/api-reference.md)** - Complete API documentation for all hooks and utilities
|
|
69
|
+
- **[Guides](./docs/guides.md)** - Advanced patterns and best practices
|
|
70
|
+
- **[Examples](./docs/examples.md)** - Real-world implementation examples
|
|
71
|
+
- **[Architecture](./docs/architecture.md)** - Internal design and data flow
|
|
72
|
+
- **[Troubleshooting](./docs/troubleshooting.md)** - Common issues and solutions
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
## 🎯 Core Hooks
|
|
76
|
+
|
|
77
|
+
### `useMCPTool`
|
|
78
|
+
The main hook for registering MCP tools with full lifecycle management.
|
|
79
|
+
|
|
80
|
+
### `useMCPGetter`
|
|
81
|
+
Simplified hook for getter tools (no input parameters).
|
|
82
|
+
|
|
83
|
+
### `useMCPAction`
|
|
84
|
+
Hook for action tools that accept inputs and perform operations.
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
[→ See full API documentation](./docs/api-reference.md)
|
|
88
|
+
|
|
89
|
+
## 🏗️ Project Links
|
|
90
|
+
|
|
91
|
+
- [📦 npm Package](https://www.npmjs.com/package/@mcp-fe/react-tools)
|
|
92
|
+
- [🐙 GitHub Repository](https://github.com/mcp-fe/mcp-fe)
|
|
93
|
+
- [🌐 Homepage](https://mcp-fe.ai)
|
|
94
|
+
- [📝 Changelog](../../CHANGELOG.md)
|
|
95
|
+
|
|
96
|
+
## 📄 License
|
|
97
|
+
|
|
98
|
+
Apache-2.0 - See [LICENSE](../../LICENSE) for details.
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
**Ready to get started?** Check out the [Getting Started Guide](./docs/getting-started.md)!
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
import { WorkerClientInitOptions } from '@mcp-fe/mcp-worker';
|
|
3
|
+
interface MCPToolsContextValue {
|
|
4
|
+
/**
|
|
5
|
+
* Whether the worker client is initialized
|
|
6
|
+
*/
|
|
7
|
+
isInitialized: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Whether connected to MCP server
|
|
10
|
+
*/
|
|
11
|
+
isConnected: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* List of currently registered tool names
|
|
14
|
+
*/
|
|
15
|
+
registeredTools: string[];
|
|
16
|
+
/**
|
|
17
|
+
* Initialize the worker client (if not already initialized)
|
|
18
|
+
*/
|
|
19
|
+
initialize: (options?: WorkerClientInitOptions) => Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Get connection status
|
|
22
|
+
*/
|
|
23
|
+
getConnectionStatus: () => Promise<boolean>;
|
|
24
|
+
}
|
|
25
|
+
export interface MCPToolsProviderProps {
|
|
26
|
+
children: React.ReactNode;
|
|
27
|
+
/**
|
|
28
|
+
* Auto-initialize on mount (default: true)
|
|
29
|
+
*/
|
|
30
|
+
autoInit?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Backend WebSocket URL
|
|
33
|
+
*/
|
|
34
|
+
backendWsUrl?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Other worker client init options
|
|
37
|
+
*/
|
|
38
|
+
initOptions?: WorkerClientInitOptions;
|
|
39
|
+
/**
|
|
40
|
+
* Callback when initialization completes
|
|
41
|
+
*/
|
|
42
|
+
onInitialized?: () => void;
|
|
43
|
+
/**
|
|
44
|
+
* Callback when initialization fails
|
|
45
|
+
*/
|
|
46
|
+
onInitError?: (error: Error) => void;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Provider for MCP Tools context
|
|
50
|
+
*
|
|
51
|
+
* @example Basic usage:
|
|
52
|
+
* ```tsx
|
|
53
|
+
* function App() {
|
|
54
|
+
* return (
|
|
55
|
+
* <MCPToolsProvider backendWsUrl="ws://localhost:3001">
|
|
56
|
+
* <YourApp />
|
|
57
|
+
* </MCPToolsProvider>
|
|
58
|
+
* );
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example With callbacks:
|
|
63
|
+
* ```tsx
|
|
64
|
+
* function App() {
|
|
65
|
+
* return (
|
|
66
|
+
* <MCPToolsProvider
|
|
67
|
+
* backendWsUrl="ws://localhost:3001"
|
|
68
|
+
* onInitialized={() => console.log('MCP Tools ready!')}
|
|
69
|
+
* onInitError={(err) => console.error('MCP init failed:', err)}
|
|
70
|
+
* >
|
|
71
|
+
* <YourApp />
|
|
72
|
+
* </MCPToolsProvider>
|
|
73
|
+
* );
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @example Manual initialization:
|
|
78
|
+
* ```tsx
|
|
79
|
+
* function App() {
|
|
80
|
+
* return (
|
|
81
|
+
* <MCPToolsProvider autoInit={false}>
|
|
82
|
+
* <YourApp />
|
|
83
|
+
* </MCPToolsProvider>
|
|
84
|
+
* );
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* function YourApp() {
|
|
88
|
+
* const { initialize } = useMCPToolsContext();
|
|
89
|
+
*
|
|
90
|
+
* return (
|
|
91
|
+
* <button onClick={() => initialize({ backendWsUrl: 'ws://localhost:3001' })}>
|
|
92
|
+
* Initialize MCP
|
|
93
|
+
* </button>
|
|
94
|
+
* );
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare function MCPToolsProvider({ children, autoInit, backendWsUrl, initOptions, onInitialized, onInitError, }: MCPToolsProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
99
|
+
/**
|
|
100
|
+
* Hook to access MCP Tools context
|
|
101
|
+
*
|
|
102
|
+
* Note: This is OPTIONAL. useMCPTool works fine without it.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```tsx
|
|
106
|
+
* function MyComponent() {
|
|
107
|
+
* const { isConnected, registeredTools } = useMCPToolsContext();
|
|
108
|
+
*
|
|
109
|
+
* return (
|
|
110
|
+
* <div>
|
|
111
|
+
* <p>Connected: {isConnected ? 'Yes' : 'No'}</p>
|
|
112
|
+
* <p>Tools: {registeredTools.join(', ')}</p>
|
|
113
|
+
* </div>
|
|
114
|
+
* );
|
|
115
|
+
* }
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* @throws Error if used outside of MCPToolsProvider (when strict mode is enabled)
|
|
119
|
+
*/
|
|
120
|
+
export declare function useMCPToolsContext(strict?: boolean): MCPToolsContextValue;
|
|
121
|
+
/**
|
|
122
|
+
* Hook to check if MCPToolsProvider is being used
|
|
123
|
+
*/
|
|
124
|
+
export declare function useHasMCPProvider(): boolean;
|
|
125
|
+
export {};
|
|
126
|
+
//# sourceMappingURL=MCPToolsContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MCPToolsContext.d.ts","sourceRoot":"","sources":["../../../../libs/react-tools/src/context/MCPToolsContext.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAMN,MAAM,OAAO,CAAC;AACf,OAAO,EAAgB,KAAK,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAEhF,UAAU,oBAAoB;IAC5B;;OAEG;IACH,aAAa,EAAE,OAAO,CAAC;IAEvB;;OAEG;IACH,WAAW,EAAE,OAAO,CAAC;IAErB;;OAEG;IACH,eAAe,EAAE,MAAM,EAAE,CAAC;IAE1B;;OAEG;IACH,UAAU,EAAE,CAAC,OAAO,CAAC,EAAE,uBAAuB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjE;;OAEG;IACH,mBAAmB,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;CAC7C;AAID,MAAM,WAAW,qBAAqB;IACpC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAE1B;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,WAAW,CAAC,EAAE,uBAAuB,CAAC;IAEtC;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,IAAI,CAAC;IAE3B;;OAEG;IACH,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CACtC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,wBAAgB,gBAAgB,CAAC,EAC/B,QAAQ,EACR,QAAe,EACf,YAAoC,EACpC,WAAW,EACX,aAAa,EACb,WAAW,GACZ,EAAE,qBAAqB,2CAkFvB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,UAAQ,GAAG,oBAAoB,CAsBvE;AAED;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,OAAO,CAG3C"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { ToolHandler } from '@mcp-fe/mcp-worker';
|
|
2
|
+
export interface UseMCPToolOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Tool name (must be unique)
|
|
5
|
+
*/
|
|
6
|
+
name: string;
|
|
7
|
+
/**
|
|
8
|
+
* Tool description for AI
|
|
9
|
+
*/
|
|
10
|
+
description: string;
|
|
11
|
+
/**
|
|
12
|
+
* JSON Schema for tool inputs
|
|
13
|
+
*/
|
|
14
|
+
inputSchema: Record<string, unknown>;
|
|
15
|
+
/**
|
|
16
|
+
* Handler function (runs in main thread with full browser access)
|
|
17
|
+
*/
|
|
18
|
+
handler: ToolHandler;
|
|
19
|
+
/**
|
|
20
|
+
* Whether to register immediately on mount (default: true)
|
|
21
|
+
*/
|
|
22
|
+
autoRegister?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Whether to unregister on unmount (default: true)
|
|
25
|
+
* Set to false if tool should persist across component unmounts
|
|
26
|
+
*/
|
|
27
|
+
autoUnregister?: boolean;
|
|
28
|
+
}
|
|
29
|
+
export interface UseMCPToolResult {
|
|
30
|
+
/**
|
|
31
|
+
* Whether the tool is currently registered
|
|
32
|
+
*/
|
|
33
|
+
isRegistered: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Manually register the tool (if autoRegister is false)
|
|
36
|
+
*/
|
|
37
|
+
register: () => Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Manually unregister the tool
|
|
40
|
+
*/
|
|
41
|
+
unregister: () => Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Current reference count (how many components are using this tool)
|
|
44
|
+
*/
|
|
45
|
+
refCount: number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Hook for registering MCP tools with automatic lifecycle management
|
|
49
|
+
*
|
|
50
|
+
* @example Basic usage:
|
|
51
|
+
* ```tsx
|
|
52
|
+
* function MyComponent() {
|
|
53
|
+
* const { isRegistered } = useMCPTool({
|
|
54
|
+
* name: 'get_time',
|
|
55
|
+
* description: 'Get current time',
|
|
56
|
+
* inputSchema: { type: 'object', properties: {} },
|
|
57
|
+
* handler: async () => ({
|
|
58
|
+
* content: [{ type: 'text', text: new Date().toISOString() }]
|
|
59
|
+
* })
|
|
60
|
+
* });
|
|
61
|
+
*
|
|
62
|
+
* return <div>Tool registered: {isRegistered ? 'Yes' : 'No'}</div>;
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @example With dependencies (React state, props, etc.):
|
|
67
|
+
* ```tsx
|
|
68
|
+
* function UserProfile({ userId }) {
|
|
69
|
+
* const [userData, setUserData] = useState(null);
|
|
70
|
+
*
|
|
71
|
+
* useMCPTool({
|
|
72
|
+
* name: 'get_user_profile',
|
|
73
|
+
* description: 'Get current user profile',
|
|
74
|
+
* inputSchema: { type: 'object', properties: {} },
|
|
75
|
+
* handler: async () => {
|
|
76
|
+
* // Has access to component state!
|
|
77
|
+
* return {
|
|
78
|
+
* content: [{
|
|
79
|
+
* type: 'text',
|
|
80
|
+
* text: JSON.stringify({ userId, userData })
|
|
81
|
+
* }]
|
|
82
|
+
* };
|
|
83
|
+
* }
|
|
84
|
+
* });
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @example Manual registration:
|
|
89
|
+
* ```tsx
|
|
90
|
+
* function AdminPanel() {
|
|
91
|
+
* const { register, unregister, isRegistered } = useMCPTool({
|
|
92
|
+
* name: 'admin_action',
|
|
93
|
+
* description: 'Perform admin action',
|
|
94
|
+
* inputSchema: { type: 'object', properties: {} },
|
|
95
|
+
* handler: async () => ({ content: [{ type: 'text', text: 'Done' }] }),
|
|
96
|
+
* autoRegister: false
|
|
97
|
+
* });
|
|
98
|
+
*
|
|
99
|
+
* return (
|
|
100
|
+
* <button onClick={isRegistered ? unregister : register}>
|
|
101
|
+
* {isRegistered ? 'Disable' : 'Enable'} Admin Tool
|
|
102
|
+
* </button>
|
|
103
|
+
* );
|
|
104
|
+
* }
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export declare function useMCPTool(options: UseMCPToolOptions): UseMCPToolResult;
|
|
108
|
+
/**
|
|
109
|
+
* Utility function to check if a tool is registered
|
|
110
|
+
*/
|
|
111
|
+
export declare function isToolRegistered(name: string): boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Utility function to get all registered tools
|
|
114
|
+
*/
|
|
115
|
+
export declare function getRegisteredTools(): string[];
|
|
116
|
+
/**
|
|
117
|
+
* Utility function to get tool info
|
|
118
|
+
*/
|
|
119
|
+
export declare function getToolInfo(name: string): {
|
|
120
|
+
refCount: number;
|
|
121
|
+
isRegistered: boolean;
|
|
122
|
+
} | null;
|
|
123
|
+
//# sourceMappingURL=useMCPTool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMCPTool.d.ts","sourceRoot":"","sources":["../../../../libs/react-tools/src/hooks/useMCPTool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAAgB,KAAK,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEpE,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAErC;;OAEG;IACH,OAAO,EAAE,WAAW,CAAC;IAErB;;OAEG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IAEvB;;;OAGG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,YAAY,EAAE,OAAO,CAAC;IAEtB;;OAEG;IACH,QAAQ,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAE9B;;OAEG;IACH,UAAU,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAEhC;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2DG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,iBAAiB,GAAG,gBAAgB,CA4HvE;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEtD;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,MAAM,EAAE,CAE7C;AAED;;GAEG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,GACX;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,OAAO,CAAA;CAAE,GAAG,IAAI,CAEpD"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { UseMCPToolOptions } from './useMCPTool';
|
|
2
|
+
/**
|
|
3
|
+
* Hook for creating a simple getter tool (no inputs, just returns data)
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```tsx
|
|
7
|
+
* function UserProfile() {
|
|
8
|
+
* const user = useUser();
|
|
9
|
+
*
|
|
10
|
+
* useMCPGetter(
|
|
11
|
+
* 'get_user_profile',
|
|
12
|
+
* 'Get current user profile',
|
|
13
|
+
* () => ({ userId: user.id, name: user.name })
|
|
14
|
+
* );
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function useMCPGetter<T = unknown>(name: string, description: string, getter: () => T | Promise<T>, options?: Partial<Omit<UseMCPToolOptions, 'name' | 'description' | 'handler' | 'inputSchema'>>): import('./useMCPTool').UseMCPToolResult;
|
|
19
|
+
/**
|
|
20
|
+
* Hook for creating an action tool (takes input, performs action)
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* function TodoList() {
|
|
25
|
+
* const [todos, setTodos] = useState([]);
|
|
26
|
+
*
|
|
27
|
+
* useMCPAction(
|
|
28
|
+
* 'add_todo',
|
|
29
|
+
* 'Add a new todo item',
|
|
30
|
+
* {
|
|
31
|
+
* text: { type: 'string', description: 'Todo text' }
|
|
32
|
+
* },
|
|
33
|
+
* async (args: { text: string }) => {
|
|
34
|
+
* const newTodo = { id: Date.now(), text: args.text, done: false };
|
|
35
|
+
* setTodos([...todos, newTodo]);
|
|
36
|
+
* return { success: true, todo: newTodo };
|
|
37
|
+
* }
|
|
38
|
+
* );
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function useMCPAction<TArgs = unknown, TResult = unknown>(name: string, description: string, properties: Record<string, unknown>, action: (args: TArgs) => TResult | Promise<TResult>, options?: Partial<Omit<UseMCPToolOptions, 'name' | 'description' | 'handler' | 'inputSchema'>> & {
|
|
43
|
+
required?: string[];
|
|
44
|
+
}): import('./useMCPTool').UseMCPToolResult;
|
|
45
|
+
//# sourceMappingURL=useMCPToolHelpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMCPToolHelpers.d.ts","sourceRoot":"","sources":["../../../../libs/react-tools/src/hooks/useMCPToolHelpers.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAc,KAAK,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAElE;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,CAAC,GAAG,OAAO,EACtC,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,EAC5B,OAAO,CAAC,EAAE,OAAO,CACf,IAAI,CAAC,iBAAiB,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS,GAAG,aAAa,CAAC,CAC5E,2CAwBF;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,YAAY,CAAC,KAAK,GAAG,OAAO,EAAE,OAAO,GAAG,OAAO,EAC7D,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,EACnD,OAAO,CAAC,EAAE,OAAO,CACf,IAAI,CAAC,iBAAiB,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS,GAAG,aAAa,CAAC,CAC5E,GAAG;IACF,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB,2CA6BF"}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2026 Michal Kopecky
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
export { useMCPTool, isToolRegistered, getRegisteredTools, getToolInfo, } from './hooks/useMCPTool';
|
|
17
|
+
export type { UseMCPToolOptions, UseMCPToolResult } from './hooks/useMCPTool';
|
|
18
|
+
export { useMCPGetter, useMCPAction } from './hooks/useMCPToolHelpers';
|
|
19
|
+
export { MCPToolsProvider, useMCPToolsContext, useHasMCPProvider, } from './context/MCPToolsContext';
|
|
20
|
+
export type { MCPToolsProviderProps } from './context/MCPToolsContext';
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../libs/react-tools/src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EACL,UAAU,EACV,gBAAgB,EAChB,kBAAkB,EAClB,WAAW,GACZ,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAE9E,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEvE,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,2BAA2B,CAAC;AACnC,YAAY,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC"}
|