@mozilla-ai/mcpd 0.0.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 +201 -0
- package/README.md +429 -0
- package/dist/apiPaths.d.ts +11 -0
- package/dist/apiPaths.d.ts.map +1 -0
- package/dist/client.d.ts +231 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/dynamicCaller.d.ts +146 -0
- package/dist/dynamicCaller.d.ts.map +1 -0
- package/dist/errors.d.ts +113 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/functionBuilder.d.ts +110 -0
- package/dist/functionBuilder.d.ts.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1130 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1130 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types.d.ts +317 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/cache.d.ts +22 -0
- package/dist/utils/cache.d.ts.map +1 -0
- package/dist/utils/typeConverter.d.ts +34 -0
- package/dist/utils/typeConverter.d.ts.map +1 -0
- package/package.json +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
https://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 authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (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 reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 Mozilla.ai
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
# mcpd-sdk-javascript
|
|
2
|
+
|
|
3
|
+
`mcpd-sdk-javascript` is a TypeScript/JavaScript SDK for interacting with the [mcpd](https://github.com/mozilla-ai/mcpd) application.
|
|
4
|
+
|
|
5
|
+
A daemon that exposes MCP server tools via a simple HTTP API.
|
|
6
|
+
|
|
7
|
+
This SDK provides high-level and dynamic access to those tools, making it easy to integrate with scripts, applications, or agentic frameworks.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Discover and list available `mcpd` hosted MCP servers
|
|
12
|
+
- Retrieve tool definitions and schemas for one or all servers
|
|
13
|
+
- Dynamically invoke any tool using a clean, attribute-based syntax
|
|
14
|
+
- Unified AI framework integration - works directly with LangChain JS and Vercel AI SDK
|
|
15
|
+
- Generate self-contained, framework-compatible tool functions without conversion layers
|
|
16
|
+
- Multiple output formats (`'array'`, `'object'`, `'map'`) for different framework needs
|
|
17
|
+
- Full TypeScript support with comprehensive type definitions and overloads
|
|
18
|
+
- Minimal dependencies (`lru-cache` for caching, `zod` for schema validation)
|
|
19
|
+
- Works in both Node.js and browser environments
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @mozilla-ai/mcpd
|
|
25
|
+
# or
|
|
26
|
+
yarn add @mozilla-ai/mcpd
|
|
27
|
+
# or
|
|
28
|
+
pnpm add @mozilla-ai/mcpd
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Quick Start
|
|
32
|
+
|
|
33
|
+
> **Note:** This SDK works seamlessly with both JavaScript and TypeScript. TypeScript users automatically get full type safety and autocomplete via the included `.d.ts` type definitions—no additional setup required.
|
|
34
|
+
|
|
35
|
+
### JavaScript
|
|
36
|
+
|
|
37
|
+
```javascript
|
|
38
|
+
const { McpdClient, McpdError } = require("@mozilla-ai/mcpd");
|
|
39
|
+
|
|
40
|
+
const client = new McpdClient({
|
|
41
|
+
apiEndpoint: "http://localhost:8090",
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// List available servers
|
|
45
|
+
const servers = await client.listServers();
|
|
46
|
+
console.log(servers);
|
|
47
|
+
// Example: ['time', 'fetch', 'git']
|
|
48
|
+
|
|
49
|
+
// List tool definitions for a specific server
|
|
50
|
+
const tools = await client.servers.time.listTools();
|
|
51
|
+
console.log(tools);
|
|
52
|
+
|
|
53
|
+
// Dynamically call a tool via the .tools namespace
|
|
54
|
+
try {
|
|
55
|
+
const result = await client.servers.time.tools.get_current_time({
|
|
56
|
+
timezone: "UTC",
|
|
57
|
+
});
|
|
58
|
+
console.log(result);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error instanceof McpdError) {
|
|
61
|
+
console.error("Error:", error.message);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### TypeScript
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
import { McpdClient, McpdError, Tool } from "@mozilla-ai/mcpd";
|
|
70
|
+
|
|
71
|
+
const client = new McpdClient({
|
|
72
|
+
apiEndpoint: "http://localhost:8090",
|
|
73
|
+
apiKey: "optional-key", // Optional API key
|
|
74
|
+
healthCacheTtl: 10, // Cache health checks for 10 seconds
|
|
75
|
+
serverCacheTtl: 60, // Cache server/tool metadata for 60 seconds
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Full type safety and autocomplete
|
|
79
|
+
const servers: string[] = await client.listServers();
|
|
80
|
+
|
|
81
|
+
// Get tools with proper typing
|
|
82
|
+
const tools: Tool[] = await client.servers.time.listTools();
|
|
83
|
+
|
|
84
|
+
// Dynamic tool invocation with error handling via .tools namespace
|
|
85
|
+
try {
|
|
86
|
+
const result = await client.servers.time.tools.get_current_time({
|
|
87
|
+
timezone: "UTC",
|
|
88
|
+
});
|
|
89
|
+
console.log(result);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (error instanceof McpdError) {
|
|
92
|
+
console.error("Operation failed:", error.message);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## API
|
|
98
|
+
|
|
99
|
+
### Initialization
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { McpdClient } from "@mozilla-ai/mcpd";
|
|
103
|
+
|
|
104
|
+
// Initialize the client with your mcpd API endpoint
|
|
105
|
+
const client = new McpdClient({
|
|
106
|
+
apiEndpoint: "http://localhost:8090", // Required
|
|
107
|
+
apiKey: "optional-key", // Optional: API key for authentication
|
|
108
|
+
healthCacheTtl: 10, // Optional: TTL in seconds for health cache (default: 10)
|
|
109
|
+
serverCacheTtl: 60, // Optional: TTL in seconds for server/tools cache (default: 60)
|
|
110
|
+
timeout: 30000, // Optional: Request timeout in ms (default: 30000)
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Core Methods
|
|
115
|
+
|
|
116
|
+
#### `client.listServers()`
|
|
117
|
+
|
|
118
|
+
Returns a list of all configured server names.
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
const servers = await client.listServers();
|
|
122
|
+
// Returns: ['time', 'fetch', 'git']
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### `client.getToolSchemas(options?)`
|
|
126
|
+
|
|
127
|
+
Returns tool schemas from all (or specific) servers with names transformed to `serverName__toolName` format.
|
|
128
|
+
|
|
129
|
+
**IMPORTANT**: Tool names are automatically transformed to prevent naming clashes and identify server origin. Original tool name `get_current_time` on server `time` becomes `time__get_current_time`.
|
|
130
|
+
|
|
131
|
+
This is useful for:
|
|
132
|
+
|
|
133
|
+
- MCP servers aggregating and re-exposing tools from multiple upstream servers
|
|
134
|
+
- Tool inspection and discovery across all servers
|
|
135
|
+
- Custom tooling that needs raw MCP tool schemas with unique names
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
// Get all tools from all servers
|
|
139
|
+
const allTools = await client.getToolSchemas();
|
|
140
|
+
// Returns: [
|
|
141
|
+
// { name: "time__get_current_time", description: "...", inputSchema: {...} },
|
|
142
|
+
// { name: "fetch__fetch_url", description: "...", inputSchema: {...} },
|
|
143
|
+
// { name: "git__commit", description: "...", inputSchema: {...} }
|
|
144
|
+
// ]
|
|
145
|
+
|
|
146
|
+
// Get tools from specific servers only
|
|
147
|
+
const someTools = await client.getToolSchemas({ servers: ["time", "fetch"] });
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### `client.servers.<server>.listTools()`
|
|
151
|
+
|
|
152
|
+
Returns tool schemas for a specific server.
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
// Get tools for a specific server
|
|
156
|
+
const timeTools = await client.servers.time.listTools();
|
|
157
|
+
// Returns: [{ name: 'get_current_time', description: '...', inputSchema: {...} }]
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
#### `client.servers.<server>.tools.<tool>(args)`
|
|
161
|
+
|
|
162
|
+
Dynamically invoke any tool using natural syntax via the `.tools` namespace. Tool names must match exactly as returned by the MCP server.
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
// Call a tool with parameters using property access (recommended)
|
|
166
|
+
const result = await client.servers.weather.tools.get_forecast({
|
|
167
|
+
city: "Tokyo",
|
|
168
|
+
days: 3,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// Call without parameters
|
|
172
|
+
const time = await client.servers.time.tools.get_current_time();
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
#### `client.servers.<server>.listTools()`
|
|
176
|
+
|
|
177
|
+
List all tools available on a specific server.
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
// List tools for a server using property access
|
|
181
|
+
const tools = await client.servers.time.listTools();
|
|
182
|
+
for (const tool of tools) {
|
|
183
|
+
console.log(`${tool.name}: ${tool.description}`);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Useful in loops with dynamic server names
|
|
187
|
+
const servers = await client.listServers();
|
|
188
|
+
for (const serverName of servers) {
|
|
189
|
+
const tools = await client.servers[serverName].listTools();
|
|
190
|
+
console.log(`${serverName}: ${tools.length} tools`);
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
#### `client.servers.<server>.callTool(toolName, args?)`
|
|
195
|
+
|
|
196
|
+
Call a tool by name with the given arguments. This is useful for programmatic tool invocation when the tool name is in a variable.
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
// Call with dynamic tool name
|
|
200
|
+
const toolName = "get_current_time";
|
|
201
|
+
const result = await client.servers.time.callTool(toolName, {
|
|
202
|
+
timezone: "UTC",
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// Using with dynamic server name too
|
|
206
|
+
const serverName = "time";
|
|
207
|
+
const result2 = await client.servers[serverName].callTool(toolName, {
|
|
208
|
+
timezone: "UTC",
|
|
209
|
+
});
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
#### `client.servers.<server>.hasTool(toolName)`
|
|
213
|
+
|
|
214
|
+
Check if a specific tool exists on a server. Tool names must match exactly as returned by the MCP server.
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
// Check if tool exists before calling it
|
|
218
|
+
if (await client.servers.time.hasTool("get_current_time")) {
|
|
219
|
+
const result = await client.servers.time.callTool("get_current_time", {
|
|
220
|
+
timezone: "UTC",
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Using with dynamic server names
|
|
225
|
+
const serverName = "time";
|
|
226
|
+
if (await client.servers[serverName].hasTool("get_current_time")) {
|
|
227
|
+
const result = await client.servers[serverName].tools.get_current_time();
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
#### `client.getServerHealth(serverName?: string)`
|
|
232
|
+
|
|
233
|
+
Get health information for one or all servers.
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
// Get health for all servers
|
|
237
|
+
const allHealth = await client.getServerHealth();
|
|
238
|
+
// Returns: { time: { status: 'ok' }, fetch: { status: 'ok' } }
|
|
239
|
+
|
|
240
|
+
// Get health for specific server
|
|
241
|
+
const timeHealth = await client.getServerHealth("time");
|
|
242
|
+
// Returns: { status: 'ok' }
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
#### `client.isServerHealthy(serverName: string)`
|
|
246
|
+
|
|
247
|
+
Check if a specific server is healthy.
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
if (await client.isServerHealthy("time")) {
|
|
251
|
+
// Server is healthy, safe to use
|
|
252
|
+
const result = await client.servers.time.tools.get_current_time({
|
|
253
|
+
timezone: "UTC",
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
#### `client.getAgentTools(options?)`
|
|
259
|
+
|
|
260
|
+
Generate callable functions that work directly with AI agent frameworks. No conversion layers needed.
|
|
261
|
+
|
|
262
|
+
```typescript
|
|
263
|
+
// Options: { servers?: string[], format?: 'array' | 'object' | 'map' }
|
|
264
|
+
// Default format is 'array' (for LangChain)
|
|
265
|
+
|
|
266
|
+
// Use with LangChain JS (array format is default)
|
|
267
|
+
import { ChatOpenAI } from "@langchain/openai";
|
|
268
|
+
|
|
269
|
+
const langchainTools = await client.getAgentTools({ format: "array" });
|
|
270
|
+
// Or simply: const langchainTools = await client.getAgentTools();
|
|
271
|
+
|
|
272
|
+
// Bind tools to model
|
|
273
|
+
const model = new ChatOpenAI({ modelName: "gpt-4o-mini" });
|
|
274
|
+
const modelWithTools = model.bindTools(langchainTools);
|
|
275
|
+
|
|
276
|
+
// Or use with agents
|
|
277
|
+
import { createOpenAIToolsAgent } from "langchain/agents";
|
|
278
|
+
const agent = await createOpenAIToolsAgent({
|
|
279
|
+
llm,
|
|
280
|
+
tools: langchainTools,
|
|
281
|
+
prompt,
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// Use with Vercel AI SDK (expects object format)
|
|
285
|
+
import { generateText } from "ai";
|
|
286
|
+
|
|
287
|
+
const vercelTools = await client.getAgentTools({ format: "object" });
|
|
288
|
+
const result = await generateText({
|
|
289
|
+
model: openai("gpt-4o-mini"),
|
|
290
|
+
tools: vercelTools,
|
|
291
|
+
prompt: "What time is it in Tokyo?",
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
// Filter to specific servers
|
|
295
|
+
const timeTools = await client.getAgentTools({
|
|
296
|
+
servers: ["time"],
|
|
297
|
+
format: "array",
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
// Use with Map for efficient lookups
|
|
301
|
+
const toolMap = await client.getAgentTools({ format: "map" });
|
|
302
|
+
const timeTool = toolMap.get("time__get_current_time");
|
|
303
|
+
if (timeTool) {
|
|
304
|
+
const result = await timeTool({ timezone: "UTC" });
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Each function has metadata for both frameworks
|
|
308
|
+
const tools = await client.getAgentTools();
|
|
309
|
+
for (const tool of tools) {
|
|
310
|
+
console.log(`${tool.name}: ${tool.description}`);
|
|
311
|
+
console.log(`Server: ${tool._serverName}, Tool: ${tool._toolName}`);
|
|
312
|
+
// LangChain properties: tool.schema, tool.invoke, tool.lc_namespace
|
|
313
|
+
// Vercel AI properties: tool.inputSchema, tool.execute
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
#### `client.clearAgentToolsCache()`
|
|
318
|
+
|
|
319
|
+
Clear the cache of generated agent tools functions.
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
// Clear cache to regenerate tools with latest schemas
|
|
323
|
+
client.clearAgentToolsCache();
|
|
324
|
+
const freshTools = await client.getAgentTools();
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
#### `client.clearServerHealthCache()`
|
|
328
|
+
|
|
329
|
+
Clear the server health cache, forcing fresh health checks on next call.
|
|
330
|
+
|
|
331
|
+
```typescript
|
|
332
|
+
// Force fresh health check
|
|
333
|
+
client.clearServerHealthCache();
|
|
334
|
+
const health = await client.getServerHealth("time");
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
## Error Handling
|
|
338
|
+
|
|
339
|
+
The SDK provides a comprehensive error hierarchy for different failure scenarios:
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
import {
|
|
343
|
+
McpdError, // Base error class
|
|
344
|
+
ConnectionError, // Cannot connect to mcpd daemon
|
|
345
|
+
AuthenticationError, // Auth failed (invalid API key)
|
|
346
|
+
ServerNotFoundError, // Server doesn't exist
|
|
347
|
+
ServerUnhealthyError, // Server is unhealthy
|
|
348
|
+
ToolNotFoundError, // Tool doesn't exist
|
|
349
|
+
ToolExecutionError, // Tool execution failed
|
|
350
|
+
ValidationError, // Input validation failed
|
|
351
|
+
TimeoutError, // Operation timed out
|
|
352
|
+
} from "@mozilla-ai/mcpd";
|
|
353
|
+
|
|
354
|
+
try {
|
|
355
|
+
const result = await client.servers.unknown.tools.tool();
|
|
356
|
+
} catch (error) {
|
|
357
|
+
if (error instanceof ToolNotFoundError) {
|
|
358
|
+
console.error(
|
|
359
|
+
`Tool not found: ${error.toolName} on server ${error.serverName}`,
|
|
360
|
+
);
|
|
361
|
+
} else if (error instanceof ConnectionError) {
|
|
362
|
+
console.error("Cannot connect to mcpd daemon. Is it running?");
|
|
363
|
+
} else if (error instanceof McpdError) {
|
|
364
|
+
console.error("Operation failed:", error.message);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
## Development
|
|
370
|
+
|
|
371
|
+
### Setup
|
|
372
|
+
|
|
373
|
+
```bash
|
|
374
|
+
# Clone the repository
|
|
375
|
+
git clone https://github.com/mozilla-ai/mcpd-sdk-javascript.git
|
|
376
|
+
cd mcpd-sdk-javascript
|
|
377
|
+
|
|
378
|
+
# Install dependencies
|
|
379
|
+
npm install
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### Building
|
|
383
|
+
|
|
384
|
+
```bash
|
|
385
|
+
# Build the project
|
|
386
|
+
npm run build
|
|
387
|
+
|
|
388
|
+
# Build in watch mode
|
|
389
|
+
npm run dev
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
### Testing
|
|
393
|
+
|
|
394
|
+
```bash
|
|
395
|
+
# Run tests
|
|
396
|
+
npm test
|
|
397
|
+
|
|
398
|
+
# Run tests with coverage
|
|
399
|
+
npm run test:coverage
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### Linting and Formatting
|
|
403
|
+
|
|
404
|
+
```bash
|
|
405
|
+
# Run linter
|
|
406
|
+
npm run lint
|
|
407
|
+
|
|
408
|
+
# Fix linting issues
|
|
409
|
+
npm run lint:fix
|
|
410
|
+
|
|
411
|
+
# Format code
|
|
412
|
+
npm run format
|
|
413
|
+
|
|
414
|
+
# Check formatting
|
|
415
|
+
npm run format:check
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
## License
|
|
419
|
+
|
|
420
|
+
Apache-2.0
|
|
421
|
+
|
|
422
|
+
## Contributing
|
|
423
|
+
|
|
424
|
+
Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to this project.
|
|
425
|
+
|
|
426
|
+
## Related Projects
|
|
427
|
+
|
|
428
|
+
- [mcpd](https://github.com/mozilla-ai/mcpd) - The MCP daemon this SDK connects to
|
|
429
|
+
- [mcpd-sdk-python](https://github.com/mozilla-ai/mcpd-sdk-python) - Python version of this SDK
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Centralized API path constants for mcpd daemon endpoints.
|
|
3
|
+
*/
|
|
4
|
+
export declare const API_PATHS: {
|
|
5
|
+
readonly SERVERS: "/api/v1/servers";
|
|
6
|
+
readonly SERVER_TOOLS: (serverName: string) => string;
|
|
7
|
+
readonly TOOL_CALL: (serverName: string, toolName: string) => string;
|
|
8
|
+
readonly HEALTH_ALL: "/api/v1/health/servers";
|
|
9
|
+
readonly HEALTH_SERVER: (serverName: string) => string;
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=apiPaths.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apiPaths.d.ts","sourceRoot":"","sources":["../src/apiPaths.ts"],"names":[],"mappings":"AAAA;;GAEG;AAOH,eAAO,MAAM,SAAS;;wCAKO,MAAM;qCAET,MAAM,YAAY,MAAM;;yCAKpB,MAAM;CAE1B,CAAC"}
|