@mokei/context-client
v0.6.0
Published
Mokei MCP client
Maintainers
Readme
Mokei MCP client
Installation
npm install @mokei/context-clientBasic Usage
import { ContextClient } from '@mokei/context-client'
import { NodeStreamsTransport } from '@enkaku/node-streams-transport'
const transport = new NodeStreamsTransport({ streams })
const client = new ContextClient({ transport })
await client.initialize()
// List available tools
const { tools } = await client.listTools()
// Call a tool (untyped)
const result = await client.callTool({
name: 'tool_name',
arguments: { key: 'value' }
})Type-Safe Usage
For the best developer experience, use type-safe clients by importing server types.
Using Server Types
If your server exports types using ExtractServerTypes from @mokei/context-server:
import type { FetchServerTypes } from '@mokei/mcp-fetch'
import { ContextClient } from '@mokei/context-client'
// Create a typed client
const client = new ContextClient<FetchServerTypes>({ transport })
await client.initialize()
// Now all tool calls are type-checked!
const result = await client.callTool({
name: 'get_markdown',
arguments: { url: 'https://example.com' } // ✓ Fully typed
})
// TypeScript will error on invalid arguments:
// arguments: { invalid: 'field' } // ✗ Compile errorCustom Context Types
You can also define custom context types manually:
import { ContextClient, type ContextTypes } from '@mokei/context-client'
type MyContextTypes = {
Tools: {
greet: { name: string }
calculate: { x: number; y: number }
}
Prompts: {
welcome: { userName: string }
}
}
const client = new ContextClient<MyContextTypes>({ transport })
await client.initialize()
// Typed tool calls
await client.callTool({
name: 'greet',
arguments: { name: 'Alice' }
})
// Typed prompt calls
await client.getPrompt({
name: 'welcome',
arguments: { userName: 'Bob' }
})Benefits of Type Safety
- Autocomplete: IDE suggests available tool/prompt names and argument fields
- Compile-time errors: Catch typos and invalid arguments before runtime
- Refactoring safety: Changes to server schemas are caught at compile time
- Documentation: Types serve as inline documentation
API Reference
Client Methods
initialize()- Initialize the client connectionlistTools()- List available tools from the servercallTool(params)- Call a tool with argumentslistPrompts()- List available prompts from the servergetPrompt(params)- Get a prompt with argumentslistResources()- List available resourcesreadResource(params)- Read a resource by URI
Type Parameters
ContextTypes- Define tool and prompt argument typesUnknownContextTypes- Default untyped context (all tools/prompts acceptRecord<string, unknown>)
