@blueberrybytes/webmcp 0.1.0
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/README.md +196 -0
- package/dist/client-handler.d.ts +121 -0
- package/dist/client-handler.js +141 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +31 -0
- package/dist/mcp-server.d.ts +100 -0
- package/dist/mcp-server.js +183 -0
- package/dist/model-context.d.ts +94 -0
- package/dist/model-context.js +199 -0
- package/dist/tool-manager.d.ts +108 -0
- package/dist/tool-manager.js +194 -0
- package/dist/types.d.ts +122 -0
- package/dist/types.js +5 -0
- package/package.json +39 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ToolManager - Utility class for managing WebMCP tools
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ToolManager = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* ToolManager provides utilities for registering and managing WebMCP tools
|
|
9
|
+
*/
|
|
10
|
+
class ToolManager {
|
|
11
|
+
constructor(modelContext) {
|
|
12
|
+
this.modelContext = modelContext;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new tool with the specified parameters
|
|
16
|
+
* @param name - The name of the tool
|
|
17
|
+
* @param description - The description of the tool
|
|
18
|
+
* @param execute - The execute function for the tool
|
|
19
|
+
* @param options - Additional options for the tool
|
|
20
|
+
* @returns The created tool
|
|
21
|
+
*/
|
|
22
|
+
createTool(name, description, execute, options) {
|
|
23
|
+
return {
|
|
24
|
+
name,
|
|
25
|
+
description,
|
|
26
|
+
execute,
|
|
27
|
+
inputSchema: options?.inputSchema,
|
|
28
|
+
annotations: options?.annotations
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Registers a tool with the model context
|
|
33
|
+
* @param tool - The tool to register
|
|
34
|
+
* @returns The registered tool
|
|
35
|
+
*/
|
|
36
|
+
registerTool(tool) {
|
|
37
|
+
try {
|
|
38
|
+
this.modelContext.registerTool(tool);
|
|
39
|
+
return tool;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
throw new Error(`Failed to register tool '${tool.name}': ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Unregisters a tool from the model context
|
|
47
|
+
* @param name - The name of the tool to unregister
|
|
48
|
+
*/
|
|
49
|
+
unregisterTool(name) {
|
|
50
|
+
this.modelContext.unregisterTool(name);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Gets a tool by name
|
|
54
|
+
* @param name - The name of the tool to retrieve
|
|
55
|
+
* @returns The tool or undefined if not found
|
|
56
|
+
*/
|
|
57
|
+
getTool(name) {
|
|
58
|
+
return this.modelContext.getTool(name);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Gets all registered tools
|
|
62
|
+
* @returns Array of all registered tools
|
|
63
|
+
*/
|
|
64
|
+
getAllTools() {
|
|
65
|
+
return this.modelContext.getAllTools();
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Gets tools filtered by category
|
|
69
|
+
* @param category - The category to filter by
|
|
70
|
+
* @returns Array of tools in the specified category
|
|
71
|
+
*/
|
|
72
|
+
getToolsByCategory(category) {
|
|
73
|
+
return this.modelContext.getToolsByCategory(category);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Gets tools filtered by tags
|
|
77
|
+
* @param tags - The tags to filter by
|
|
78
|
+
* @returns Array of tools with the specified tags
|
|
79
|
+
*/
|
|
80
|
+
getToolsByTags(tags) {
|
|
81
|
+
return this.modelContext.getToolsByTags(tags);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Creates a tool with category annotation
|
|
85
|
+
* @param name - The name of the tool
|
|
86
|
+
* @param description - The description of the tool
|
|
87
|
+
* @param category - The category of the tool
|
|
88
|
+
* @param execute - The execute function for the tool
|
|
89
|
+
* @param options - Additional options for the tool
|
|
90
|
+
* @returns The created tool
|
|
91
|
+
*/
|
|
92
|
+
createCategorizedTool(name, description, category, execute, options) {
|
|
93
|
+
return this.createTool(name, description, execute, {
|
|
94
|
+
inputSchema: options?.inputSchema,
|
|
95
|
+
annotations: {
|
|
96
|
+
category,
|
|
97
|
+
tags: options?.tags
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Creates a tool with authentication requirements
|
|
103
|
+
* @param name - The name of the tool
|
|
104
|
+
* @param description - The description of the tool
|
|
105
|
+
* @param execute - The execute function for the tool
|
|
106
|
+
* @param options - Additional options for the tool
|
|
107
|
+
* @returns The created tool
|
|
108
|
+
*/
|
|
109
|
+
createAuthenticatedTool(name, description, execute, options) {
|
|
110
|
+
return this.createTool(name, description, execute, {
|
|
111
|
+
inputSchema: options?.inputSchema,
|
|
112
|
+
annotations: {
|
|
113
|
+
requiresAuth: true,
|
|
114
|
+
scopes: options?.scopes,
|
|
115
|
+
tags: options?.tags
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Creates a read-only tool
|
|
121
|
+
* @param name - The name of the tool
|
|
122
|
+
* @param description - The description of the tool
|
|
123
|
+
* @param execute - The execute function for the tool
|
|
124
|
+
* @param options - Additional options for the tool
|
|
125
|
+
* @returns The created tool
|
|
126
|
+
*/
|
|
127
|
+
createReadOnlyTool(name, description, execute, options) {
|
|
128
|
+
return this.createTool(name, description, execute, {
|
|
129
|
+
inputSchema: options?.inputSchema,
|
|
130
|
+
annotations: {
|
|
131
|
+
readOnlyHint: true,
|
|
132
|
+
tags: options?.tags
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Batch registers multiple tools
|
|
138
|
+
* @param tools - Array of tools to register
|
|
139
|
+
* @returns Array of successfully registered tools
|
|
140
|
+
*/
|
|
141
|
+
registerTools(tools) {
|
|
142
|
+
const registeredTools = [];
|
|
143
|
+
const errors = [];
|
|
144
|
+
for (const tool of tools) {
|
|
145
|
+
try {
|
|
146
|
+
this.registerTool(tool);
|
|
147
|
+
registeredTools.push(tool);
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
errors.push(`Failed to register tool '${tool.name}': ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (errors.length > 0) {
|
|
154
|
+
console.warn('Tool registration completed with errors:', errors.join('\n'));
|
|
155
|
+
}
|
|
156
|
+
return registeredTools;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Validates a tool before registration
|
|
160
|
+
* @param tool - The tool to validate
|
|
161
|
+
* @returns True if the tool is valid, false otherwise
|
|
162
|
+
*/
|
|
163
|
+
validateTool(tool) {
|
|
164
|
+
// Check required fields
|
|
165
|
+
if (!tool.name || typeof tool.name !== 'string') {
|
|
166
|
+
console.error('Tool must have a valid name');
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
if (!tool.description || typeof tool.description !== 'string') {
|
|
170
|
+
console.error(`Tool '${tool.name}' must have a valid description`);
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
if (!tool.execute || typeof tool.execute !== 'function') {
|
|
174
|
+
console.error(`Tool '${tool.name}' must have a valid execute function`);
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
// Validate input schema if provided
|
|
178
|
+
if (tool.inputSchema) {
|
|
179
|
+
if (typeof tool.inputSchema !== 'object' || tool.inputSchema === null) {
|
|
180
|
+
console.error(`Tool '${tool.name}' has an invalid input schema`);
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// Validate annotations if provided
|
|
185
|
+
if (tool.annotations) {
|
|
186
|
+
if (typeof tool.annotations !== 'object' || tool.annotations === null) {
|
|
187
|
+
console.error(`Tool '${tool.name}' has invalid annotations`);
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
exports.ToolManager = ToolManager;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebMCP Types - TypeScript definitions for the Web Model Context Protocol
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Represents an AI agent that can interact with WebMCP tools
|
|
6
|
+
*/
|
|
7
|
+
export interface ModelContextClient {
|
|
8
|
+
/**
|
|
9
|
+
* Asynchronously requests user input during the execution of a tool
|
|
10
|
+
* @param callback - Function to perform the user interaction
|
|
11
|
+
* @returns Promise that resolves with the result of the callback
|
|
12
|
+
*/
|
|
13
|
+
requestUserInteraction<T>(callback: () => Promise<T>): Promise<T>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* JSON Schema for tool input validation
|
|
17
|
+
*/
|
|
18
|
+
export interface JSONSchema {
|
|
19
|
+
type: string;
|
|
20
|
+
properties?: Record<string, any>;
|
|
21
|
+
required?: string[];
|
|
22
|
+
[key: string]: any;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Annotations providing additional metadata about a tool's behavior
|
|
26
|
+
*/
|
|
27
|
+
export interface ToolAnnotations {
|
|
28
|
+
/**
|
|
29
|
+
* If true, indicates that the tool does not modify any state and only reads data
|
|
30
|
+
*/
|
|
31
|
+
readOnlyHint?: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Tool category for organization and filtering
|
|
34
|
+
*/
|
|
35
|
+
category?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Tags for tool discovery and filtering
|
|
38
|
+
*/
|
|
39
|
+
tags?: string[];
|
|
40
|
+
/**
|
|
41
|
+
* Authentication requirements for the tool
|
|
42
|
+
*/
|
|
43
|
+
requiresAuth?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Scopes required for tool execution
|
|
46
|
+
*/
|
|
47
|
+
scopes?: string[];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Represents a tool that can be invoked by agents
|
|
51
|
+
*/
|
|
52
|
+
export interface ModelContextTool {
|
|
53
|
+
/**
|
|
54
|
+
* Unique identifier for the tool
|
|
55
|
+
*/
|
|
56
|
+
name: string;
|
|
57
|
+
/**
|
|
58
|
+
* Natural language description of the tool's functionality
|
|
59
|
+
*/
|
|
60
|
+
description: string;
|
|
61
|
+
/**
|
|
62
|
+
* JSON Schema describing the expected input parameters for the tool
|
|
63
|
+
*/
|
|
64
|
+
inputSchema?: JSONSchema;
|
|
65
|
+
/**
|
|
66
|
+
* Callback function that is invoked when an agent calls the tool
|
|
67
|
+
* @param input - Input parameters for the tool
|
|
68
|
+
* @param client - ModelContextClient representing the agent
|
|
69
|
+
* @returns Promise that resolves with the tool result
|
|
70
|
+
*/
|
|
71
|
+
execute: (input: Record<string, any>, client: ModelContextClient) => Promise<any>;
|
|
72
|
+
/**
|
|
73
|
+
* Optional annotations providing additional metadata about the tool's behavior
|
|
74
|
+
*/
|
|
75
|
+
annotations?: ToolAnnotations;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Options for providing context to the ModelContext
|
|
79
|
+
*/
|
|
80
|
+
export interface ModelContextOptions {
|
|
81
|
+
/**
|
|
82
|
+
* List of tools to register with the browser
|
|
83
|
+
*/
|
|
84
|
+
tools?: ModelContextTool[];
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Content types supported in tool results
|
|
88
|
+
*/
|
|
89
|
+
export type ToolResultContent = {
|
|
90
|
+
type: "text";
|
|
91
|
+
text: string;
|
|
92
|
+
} | {
|
|
93
|
+
type: "image";
|
|
94
|
+
data: string;
|
|
95
|
+
mimeType: string;
|
|
96
|
+
alt?: string;
|
|
97
|
+
} | {
|
|
98
|
+
type: "html";
|
|
99
|
+
html: string;
|
|
100
|
+
} | {
|
|
101
|
+
type: "link";
|
|
102
|
+
url: string;
|
|
103
|
+
title?: string;
|
|
104
|
+
} | {
|
|
105
|
+
type: "table";
|
|
106
|
+
headers: string[];
|
|
107
|
+
rows: any[][];
|
|
108
|
+
} | {
|
|
109
|
+
type: "error";
|
|
110
|
+
message: string;
|
|
111
|
+
code?: string;
|
|
112
|
+
} | {
|
|
113
|
+
type: "resource";
|
|
114
|
+
uri: string;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Result returned by a tool execution
|
|
118
|
+
*/
|
|
119
|
+
export interface ToolResult {
|
|
120
|
+
content?: ToolResultContent[];
|
|
121
|
+
[key: string]: any;
|
|
122
|
+
}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@blueberrybytes/webmcp",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "TypeScript library for WebMCP - Web Model Context Protocol",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/**/*"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"clean": "rm -rf dist",
|
|
13
|
+
"prebuild": "npm run clean",
|
|
14
|
+
"test": "jest",
|
|
15
|
+
"prepublishOnly": "npm run build",
|
|
16
|
+
"pub": "npm publish --access public",
|
|
17
|
+
"pub:beta": "npm publish --access public --tag beta"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"webmcp",
|
|
21
|
+
"mcp",
|
|
22
|
+
"ai",
|
|
23
|
+
"agent",
|
|
24
|
+
"tools",
|
|
25
|
+
"typescript"
|
|
26
|
+
],
|
|
27
|
+
"author": "BlueberryBytes",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"typescript": "^5.0.0",
|
|
31
|
+
"jest": "^29.0.0",
|
|
32
|
+
"ts-jest": "^29.0.0",
|
|
33
|
+
"@types/jest": "^29.0.0",
|
|
34
|
+
"@types/node": "^18.0.0"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"json-rpc-2.0": "^1.0.0"
|
|
38
|
+
}
|
|
39
|
+
}
|