webmcp-core

Python SDK

Feature-complete Python implementation using Pydantic. Perfect for data science workflows, automation scripts, and AI pipelines. Beautiful Python API with comprehensive type hints, async/await support, and identical 67.6% token reduction capabilities.

8.2K+
Downloads
1.8K
GitHub Stars
Python 3.8+
Compatibility
67.6%
Token Reduction

Quick Install

pip install webmcp-core

Python-First Features

Built specifically for Python developers and data scientists

Pydantic Data Models

Beautiful Python API with comprehensive type hints and automatic validation

Async/Await Support

High-performance processing with asyncio for concurrent optimization tasks

Data Science Integration

Perfect for Jupyter notebooks, pandas workflows, and ML pipeline integration

Same Optimization Power

67.6% token reduction with identical algorithms to the JavaScript version

Python Ecosystem Integration

Works seamlessly with your favorite Python tools and frameworks

Jupyter Notebooks

Interactive data analysis with webMCP optimization in notebook cells

Pandas Integration

Process DataFrames with web form data for large-scale analysis

FastAPI/Flask

Build web APIs with webMCP optimization endpoints

Automation Scripts

Perfect for scheduled tasks, batch processing, and CI/CD pipelines

Python Code Examples

Real-world Python examples for data science and automation workflows

Basic Usage

from webmcp_core import WebMCPProcessor, OptimizationOptions

# Initialize processor
processor = WebMCPProcessor()

# Parse HTML content
html_content = """
<form data-webmcp-goal="user login">
    <input type="email" name="email" data-webmcp-role="input.email">
    <input type="password" name="password" data-webmcp-role="input.password">
    <button type="submit" data-webmcp-role="button.submit">Login</button>
</form>
"""

webmcp_data = processor.parse_webmcp(html_content)

# Optimize for AI processing
options = OptimizationOptions(
    target_model='gpt-4o',
    compression_level='advanced',
    goal='User authentication with email and password'
)

result = processor.optimize_for_ai(webmcp_data.elements, options)
print(f"Token reduction: {result.token_reduction:.1f}%")
print(f"Cost savings: ${result.cost_savings:.4f}")

Async Processing

import asyncio
from webmcp_core import AsyncWebMCPProcessor
from typing import List

async def process_forms_batch(html_files: List[str]) -> dict:
    processor = AsyncWebMCPProcessor()
    
    # Process multiple files concurrently
    tasks = []
    for html_file in html_files:
        with open(html_file, 'r') as f:
            content = f.read()
        
        task = processor.optimize_async(content, {
            'target_model': 'claude-3.5-sonnet',
            'compression_level': 'premium'
        })
        tasks.append(task)
    
    # Wait for all optimizations to complete
    results = await asyncio.gather(*tasks)
    
    # Aggregate statistics
    total_reduction = sum(r.token_reduction for r in results)
    total_savings = sum(r.cost_savings for r in results)
    
    return {
        'total_files': len(results),
        'average_reduction': total_reduction / len(results),
        'total_cost_savings': total_savings,
        'processing_time': sum(r.processing_time for r in results)
    }

# Run batch processing
stats = asyncio.run(process_forms_batch(['form1.html', 'form2.html', 'form3.html']))

Data Science Integration

import pandas as pd
from webmcp_core import WebMCPProcessor
import matplotlib.pyplot as plt

# Load form data from CSV
df = pd.read_csv('web_forms_data.csv')

processor = WebMCPProcessor()
optimization_results = []

# Process each form in the dataset
for index, row in df.iterrows():
    result = processor.optimize_for_ai(
        elements=row['html_content'],
        options={
            'target_model': row['target_model'],
            'compression_level': 'advanced'
        }
    )
    
    optimization_results.append({
        'form_id': row['form_id'],
        'original_tokens': result.original_tokens,
        'optimized_tokens': result.optimized_tokens,
        'token_reduction': result.token_reduction,
        'cost_savings': result.cost_savings,
        'processing_time': result.processing_time
    })

# Create results DataFrame
results_df = pd.DataFrame(optimization_results)

# Analyze performance
print(f"Average token reduction: {results_df['token_reduction'].mean():.1f}%")
print(f"Total cost savings: ${results_df['cost_savings'].sum():.4f}")

# Visualize results
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.hist(results_df['token_reduction'], bins=20, alpha=0.7)
plt.title('Token Reduction Distribution')
plt.xlabel('Token Reduction (%)')

plt.subplot(1, 2, 2)
plt.scatter(results_df['original_tokens'], results_df['token_reduction'])
plt.title('Token Reduction vs Original Size')
plt.xlabel('Original Tokens')
plt.ylabel('Token Reduction (%)')
plt.tight_layout()
plt.show()

FastAPI Web Service

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from webmcp_core import WebMCPProcessor, OptimizationOptions
from typing import Optional

app = FastAPI(title="webMCP Optimization API")
processor = WebMCPProcessor()

class OptimizationRequest(BaseModel):
    html_content: str
    target_model: str = 'gpt-4o'
    compression_level: str = 'advanced'
    goal: Optional[str] = None

class OptimizationResponse(BaseModel):
    success: bool
    original_tokens: int
    optimized_tokens: int
    token_reduction: float
    cost_savings: float
    processing_time: float
    optimized_prompt: str

@app.post("/optimize", response_model=OptimizationResponse)
async def optimize_html(request: OptimizationRequest):
    try:
        # Parse webMCP data
        webmcp_data = processor.parse_webmcp(request.html_content)
        
        # Create optimization options
        options = OptimizationOptions(
            target_model=request.target_model,
            compression_level=request.compression_level,
            goal=request.goal
        )
        
        # Optimize for AI
        result = processor.optimize_for_ai(webmcp_data.elements, options)
        
        return OptimizationResponse(
            success=True,
            original_tokens=result.original_tokens,
            optimized_tokens=result.optimized_tokens,
            token_reduction=result.token_reduction,
            cost_savings=result.cost_savings,
            processing_time=result.processing_time,
            optimized_prompt=result.optimized_prompt
        )
    
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

@app.get("/health")
async def health_check():
    return {"status": "healthy", "version": "2.1.3"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

Data Science Compatibility

Full compatibility with popular data science and machine learning tools

ToolIntegrationCompatibility
JupyterInteractive notebooks with webMCP cells100%
PandasDataFrame integration for batch processing100%
NumPyNumerical analysis of optimization metrics100%
MatplotlibVisualization of token reduction patterns100%
Scikit-learnML pipeline integration for form analysis100%
StreamlitWeb app dashboards with webMCP widgets100%

Ready for Python Development?

Install webmcp-core and bring AI optimization to your Python workflows