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/core

React Components (Optional)

npm install @webmcp/react

Code 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 parse
optionsParseOptionsOptional parsing configuration

Returns:

WebMCPData

optimizeForAI(elements, options)

Optimize webMCP elements for AI model consumption

Parameters:

elementsWebMCPElement[]Array of webMCP elements
optionsOptimizationOptionsOptimization configuration

Returns:

OptimizedResult

validateSchema(data)

Validate webMCP data against schema

Parameters:

dataWebMCPDatawebMCP data to validate

Returns:

ValidationResult

calculateTokens(data, model)

Calculate token count for specific AI model

Parameters:

dataWebMCPDatawebMCP data
modelstringAI model name (gpt-4o, claude-3.5-sonnet, etc.)

Returns:

TokenCalculation

Configuration Options

Customize webMCP behavior with these configuration options

OptionTypeDefaultDescription
compression_levelstringstandardCompression level: basic, standard, advanced
Values: basic, standard, advanced
target_modelstringgpt-4oTarget AI model for optimization
Values: gpt-4o, claude-3.5-sonnet, gpt-4, gpt-3.5-turbo, gemini-pro
enableCSRFbooleanfalseEnable CSRF token protection
encryptionstringnoneEncryption method for sensitive data
Values: none, aes, jwe
cacheEnabledbooleantrueEnable LRU/TTL caching system

Ready to Build?

Start building AI-optimized web experiences with the JavaScript SDK