JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q85177F
  • License MIT

Structured parsing of MCP responses

Package Exports

  • @meldscience/mcp-stream-parser
  • @meldscience/mcp-stream-parser/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@meldscience/mcp-stream-parser) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

MCP Stream Parser

Extends the Anthropic SDK to provide structured parsing of Claude's responses. It processes Claude's streaming responses into clearly delineated types of messages while maintaining full compatibility with the SDK's types and events.

Features

  • Structured Message Parsing

    • Regular messages (plain text)
    • Thought messages (Claude's reasoning)
    • Tool messages (function calls)
    • Image content handling
  • Dual Operating Modes

    • Message Mode: Complete semantic units for platforms like Discord/Slack
    • Streaming Mode: Real-time updates for interactive UIs
  • Type-Safe Event System

    • Full SDK type compatibility
    • Strongly-typed events
    • Error recovery support

Installation

npm install @meldscience/mcp-stream-parser

Quick Start

import { MCPStreamParser } from '@meldscience/mcp-stream-parser';
import { Client } from '@anthropic-ai/sdk';

// Initialize the parser
const parser = new MCPStreamParser();

// Handle different message types
parser.on('message', ({ content }) => {
  console.log('Message:', content);
});

parser.on('thought', ({ content, hasToolCall }) => {
  console.log('Thought:', content);
});

parser.on('tool', ({ name, params, thoughtContent }) => {
  console.log('Tool:', name, params);
});

// Process a Claude stream
const client = new Client(process.env.ANTHROPIC_API_KEY);
const response = await client.messages.create({
  model: 'claude-3-opus-20240229',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Hello!' }],
  stream: true
});

await parser.processStream(response.stream);

Configuration

const parser = new MCPStreamParser({
  // Parser mode
  mode: 'message', // or 'stream'

  // Message handling (for platforms like Discord)
  message: {
    maxLength: 2000,
    overflow: 'split',
    splitPoints: ['\n\n', '. ', ' ']
  },

  // Buffer safety
  buffer: {
    maxSize: 64 * 1024,
    overflow: 'error'
  }
});

Documentation

Error Handling

parser.on('error', ({ error, fatal, partialContent }) => {
  if (fatal) {
    console.error('Fatal error:', error);
    parser.dispose();
  } else {
    console.warn('Recoverable error:', error);
    if (partialContent) {
      console.log('Partial content:', partialContent);
    }
  }
});

Features

Message Types

  • Regular Messages: Plain text content from Claude
  • Thought Messages: Claude's internal reasoning
  • Tool Messages: Function calls and tool usage
  • Image Content: Pass-through handling

Operating Modes

  • Message Mode: Complete semantic units
  • Streaming Mode: Real-time character updates

Event System

  • Text events (streaming)
  • Message events (complete units)
  • Thought events (reasoning)
  • Tool events (function calls)
  • Error events (with recovery)

License

MIT License - See LICENSE file for details