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
A lightweight parser for Claude's streaming responses that detects and structures three types of messages:
- Text messages (regular content)
- Thought messages (
{"thought": "..."}) - Tool usage messages (
{"tool": {...}})
Installation
npm install @meldscience/mcp-stream-parserQuick Start
import { MCPStreamParser } from '@meldscience/mcp-stream-parser';
import { Anthropic } from '@anthropic-ai/sdk';
const parser = new MCPStreamParser();
const anthropic = new Anthropic({ apiKey: 'your-api-key' });
// Set up handlers for each message type
parser.onMessage(text => {
console.log('Text:', text); // e.g., "Let me help you with that."
});
parser.onThought(thought => {
console.log('Thought:', thought); // e.g., "I should search the codebase first"
});
parser.onTool(tool => {
console.log('Tool:', tool); // e.g., { name: "codebase_search", params: { query: "..." } }
});
// Optional error handling
parser.onError(error => {
console.error('Error:', error);
});
// Process Claude's response
const response = await anthropic.messages.create({
model: 'claude-3-sonnet-20240229',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello!' }],
stream: true
});
await parser.processStream(response);Features
- Real-time Processing: Emits each message as soon as it's complete
- Minimal Buffering: Only buffers between clear message boundaries
- Type Safety: Full TypeScript support with exported types
- Error Handling: Continues processing after errors, with optional strict mode
Message Types
Text Messages
Regular content from Claude:
Let me help you with that.Thought Messages
Claude's internal thought process:
{"thought": "I should search the codebase first"}Tool Usage
Claude using a tool:
{"tool": {
"name": "codebase_search",
"params": {
"query": "find main function"
}
}}Configuration
const parser = new MCPStreamParser({
debug: false, // Enable debug logging
strict: false // Strict mode fails on any error
});Requirements
- Node.js >= 18
- ESM module support
License
MIT