JavaScript API
Complete reference for the webMCP JavaScript/TypeScript SDK. Build AI-optimized web experiences.
Installation
Install the JavaScript SDK via npm or yarn
Core Library
npm install @webmcp/coreReact Components (Optional)
npm install @webmcp/reactCode Examples
Learn by example with these common use cases
Basic Usage
Simple HTML to webMCP conversion
import { WebMCPProcessor } from '@webmcp/core';
const processor = new WebMCPProcessor();
const htmlContent = `<form>
<input name="email" type="email" />
<button type="submit">Login</button>
</form>`;
const webmcpData = processor.parseWebMCP(htmlContent);
console.log(webmcpData.elements);AI Optimization
Optimize for specific AI models
const optimized = processor.optimizeForAI(webmcpData.elements, {
target_model: 'gpt-4o',
compression_level: 'advanced',
goal_description: 'User login with email and password'
});
// Results in 67.6% token reduction
console.log('Token savings:', optimized.tokenReduction);
console.log('Cost savings:', optimized.costSavings);React Integration
Using webMCP with React components
import { WebMCPForm, WebMCPInput, useWebMCP } from '@webmcp/react';
function LoginForm() {
const { analytics } = useWebMCP({
targetModel: 'gpt-4o',
compressionLevel: 'advanced'
});
return (
<WebMCPForm goalDescription="User authentication">
<WebMCPInput name="email" type="email" aiSuggestions={true} />
<WebMCPInput name="password" type="password" />
<button type="submit">Login</button>
</WebMCPForm>
);
}Security Features
Enable CSRF protection and encryption
const secureProcessor = new WebMCPProcessor({
enableCSRF: true,
encryption: 'jwe',
jwtConfig: {
secret: process.env.JWT_SECRET,
expiresIn: '1h',
scopes: ['auth:login', 'user:read']
}
});
const secureData = secureProcessor.parseWebMCP(htmlContent, {
securityLevel: 'high',
sensitiveFields: ['password', 'ssn', 'creditCard']
});API Methods
Complete reference for all available methods
parseWebMCP(html, options?)
Parse HTML content and convert to webMCP format
Parameters:
htmlstringHTML content to parseoptionsParseOptionsOptional parsing configurationReturns:
WebMCPDataoptimizeForAI(elements, options)
Optimize webMCP elements for AI model consumption
Parameters:
elementsWebMCPElement[]Array of webMCP elementsoptionsOptimizationOptionsOptimization configurationReturns:
OptimizedResultvalidateSchema(data)
Validate webMCP data against schema
Parameters:
dataWebMCPDatawebMCP data to validateReturns:
ValidationResultcalculateTokens(data, model)
Calculate token count for specific AI model
Parameters:
dataWebMCPDatawebMCP datamodelstringAI model name (gpt-4o, claude-3.5-sonnet, etc.)Returns:
TokenCalculationConfiguration Options
Customize webMCP behavior with these configuration options
| Option | Type | Default | Description |
|---|---|---|---|
compression_level | string | standard | Compression level: basic, standard, advanced Values: basic, standard, advanced |
target_model | string | gpt-4o | Target AI model for optimization Values: gpt-4o, claude-3.5-sonnet, gpt-4, gpt-3.5-turbo, gemini-pro |
enableCSRF | boolean | false | Enable CSRF token protection |
encryption | string | none | Encryption method for sensitive data Values: none, aes, jwe |
cacheEnabled | boolean | true | Enable LRU/TTL caching system |
Ready to Build?
Start building AI-optimized web experiences with the JavaScript SDK