privacy_http_sdk 1.0.9

Privacy HTTP SDK for Rust
Documentation
const express = require('express');
const helmet = require('helmet');
const randomUseragent = require('random-useragent');
const fetch = require('node-fetch');
const wasmModule = require('./pkg/your_wasm_module');
const OpenAI = require ('openai');
const { BedrockRuntimeClient } = require("@aws-sdk/client-bedrock-runtime");
const { GoogleGenerativeAI } = require("@google/generative-ai");
const axios = require('axios');
const { McpServer, ResourceTemplate } = require("McpServer");
const { StdioServerTransport } = require("McpServer");
const { z } = require('zod');

const rateLimit = require('express-rate-limit');
 main


const app = express();
app.use(express.json());
const PORT = process.env.PORT || 3000;

// DID-NOSTR Verification Helper
const verifyNostrIdentity = (req) => {
  const didHeader = req.headers['x-did'];
  const signatureHeader = req.headers['x-signature'];

  if (!didHeader || !signatureHeader) return { valid: false, error: "Missing DID or Signature headers" };

  try {
    const pubkey = wasmModule.JsNostrPublicKey.from_hex(didHeader.replace('did:nostr:', ''));
    const signature = wasmModule.JsNostrSignature.from_hex(signatureHeader);
    const canonical = wasmModule.JsRequestCanonicalizer.canonicalize(
      req.method,
      req.path,
      Object.entries(req.headers),
      JSON.stringify(req.body)
    );
    return wasmModule.JsNostrVerifier.verify(pubkey, canonical, signature);
  } catch (e) {
    return { valid: false, error: e.message };
  }
};

// Create an MCP server
const server = new McpServer({
  name: "Demo",
  version: "1.0.0"
});

// Add an addition tool
server.tool("add",
  { a: z.number(), b: z.number() },
  async ({ a, b }) => ({
    content: [{ type: "text", text: String(a + b) }]
  })
);

// Add a dynamic greeting resource
server.resource(
  "greeting",
  new ResourceTemplate("greeting://{name}", { list: undefined }),
  async (uri, { name }) => ({
    contents: [{
      uri: uri.href,
      text: `Hello, ${name}!`
    }]
  })
);

// Start receiving messages on stdin and sending messages on stdout
(async () => {
  const transport = new StdioServerTransport();
  await server.connect(transport);
})();

// Middleware: Set secure HTTP headers using Helmet
app.use(helmet());

// Configure rate limiter for the JSON-RPC endpoint
const jsonRpcLimiter = rateLimit({
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 100, // limit each IP to 100 requests per windowMs
  message: { jsonrpc: '2.0', error: { code: -32009, message: 'Too many requests' } },
  standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
  legacyHeaders: false, // Disable the `X-RateLimit-*` headers
});

// Define route to call the AI API
app.get('/api/ai', async (req, res) => {
    try {
        // Generate a random user-agent
        const userAgent = randomUseragent.getRandom();
        
        // Example AI API request with the custom User-Agent header
        const aiApiUrl = 'https://api.x.ai/v1'; // Replace with your AI API URL
        const MoonshootAPI= 'https://api.moonshot.ai/v1';
        const apiXai = 'https://api.x.ai/v1';
        const apiOpenai = 'https://api.openai.com/v1';
        const QwenAPI = 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/chat/completions';
        const Claude = 'https://claude.ai/api/';
        const ollama = 'http://localhost:11434/api/generate';
        const stabilityClient = new wasmModule.HttpClient("your-stability-ai-api-key");
        const imageBase64 = stabilityClient.generate_image_sync("A serene landscape", 512, 512, 50);
        const genAI = new GoogleGenerativeAI(process.env.API_KEY);
        const inputData = { message: "Hello from WASM!" };
        // Optional: Perform preprocessing or data manipulation using WASM
        const processedData = wasmModule.process_message(JSON.stringify(inputData));
         
        // Prompt AI example
        const Prompt = {
            prompt: 'prompt text'
        }
        // Make the HTTP request to the AI API
        const response = await fetch(aiApiUrl, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'User-Agent': userAgent,
            },
            body: processedData,
        });

        if (!response.ok) {
            throw new Error(`API responded with status ${response.status}`);
        }

        const apiData = await response.json();
        res.json(apiData);
    } catch (error) {
        console.error('Error calling AI API:', error);
        res.status(500).json({ error: 'Failed to fetch data from AI API' });
    }
});

// A2A AgentCard
const agentCard = {
  name: "PrivacyServerJS",
  description: "Privacy-focused HTTP server with A2A support",
  url: "http://localhost:3000",
  version: "1.0.0",
  capabilities: {
    streaming: false,
    pushNotifications: false,
    stateTransitionHistory: false
  }
};

// A2A AgentCard endpoint
app.get('/.well-known/agent.json', (req, res) => {
  res.json(agentCard);
});

// A2A tasks/send endpoint (JSON-RPC) - with rate limiting
app.post('/', jsonRpcLimiter, (req, res) => {
  const { jsonrpc, id, method, params } = req.body;

  // Verify DID-NOSTR Identity
  const identity = verifyNostrIdentity(req);
  if (!identity.valid && process.env.REQUIRE_DID === 'true') {
    return res.status(401).json({ jsonrpc: '2.0', error: { code: -32000, message: 'Unauthorized: Invalid DID Signature' }, id });
  }

  if (jsonrpc !== '2.0' || !id || !method || !params) {
    return res.status(400).json({ jsonrpc: '2.0', error: { code: -32600, message: 'Invalid Request' }, id });
  }
  if (method === 'tasks/send') {
    const { message } = params;
    const text = message?.parts?.[0]?.text || 'No text provided';
    const response = {
      jsonrpc: '2.0',
      id,
      result: {
        id: params.id || 'task-' + Date.now(),
        status: { state: 'completed', timestamp: new Date().toISOString() },
        artifacts: [{ parts: [{ type: 'text', text: `Processed: ${text} (Verified DID: ${identity.did?.to_string() || 'None'})` }], index: 0 }]
      }
    };
    return res.json(response);
  }
  res.status(400).json({ jsonrpc: '2.0', error: { code: -32601, message: 'Method not found' }, id });
});

// Basic HTTP endpoint
app.get('/', (req, res) => {
  res.json({ message: 'Privacy-focused HTTP server with A2A support' });
});

// Start server
app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});