mcp-client
Connect to and consume MCP servers from applications. Use when: integrating MCP servers into your app, building AI agents that use MCP tools, testing MCP server implementations, or programmatically calling MCP tools and reading resources.
What this skill does
# MCP Client
## Overview
The MCP client SDK lets you connect to any MCP server, discover its capabilities (tools, resources, prompts), and invoke them programmatically. Use this when building AI agents, testing MCP servers, or integrating MCP capabilities into your application.
## Instructions
### Step 1: Install the SDK
**TypeScript:**
```bash
npm install @modelcontextprotocol/sdk
```
**Python:**
```bash
pip install mcp
```
### Step 2: Connect to a stdio server
**TypeScript:**
```typescript
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
const client = new Client(
{ name: "my-client", version: "1.0.0" },
{ capabilities: {} }
);
const transport = new StdioClientTransport({
command: "node",
args: ["path/to/server/dist/index.js"],
env: { API_KEY: process.env.API_KEY! },
});
await client.connect(transport);
console.log("Connected to MCP server");
```
**Python:**
```python
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
server_params = StdioServerParameters(
command="python",
args=["path/to/server.py"],
env={"API_KEY": "your-key"},
)
async def main():
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
# Use session here
print("Connected to MCP server")
asyncio.run(main())
```
### Step 3: Connect to an SSE server (remote)
**TypeScript:**
```typescript
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
const transport = new SSEClientTransport(
new URL("http://localhost:3000/sse")
);
await client.connect(transport);
```
**Python:**
```python
from mcp.client.sse import sse_client
async with sse_client("http://localhost:3000/sse") as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
```
### Step 4: Discover tools
```typescript
const toolsResponse = await client.listTools();
console.log("Available tools:", toolsResponse.tools);
// toolsResponse.tools is an array of:
// {
// name: string,
// description: string,
// inputSchema: JSONSchema
// }
for (const tool of toolsResponse.tools) {
console.log(`- ${tool.name}: ${tool.description}`);
}
```
**Python:**
```python
tools = await session.list_tools()
for tool in tools.tools:
print(f"- {tool.name}: {tool.description}")
```
### Step 5: Call a tool
```typescript
const result = await client.callTool({
name: "get_weather",
arguments: {
city: "Berlin",
units: "celsius",
},
});
// result.content is an array of content blocks
for (const block of result.content) {
if (block.type === "text") {
console.log(block.text);
}
}
// Check for errors
if (result.isError) {
console.error("Tool returned an error:", result.content);
}
```
**Python:**
```python
result = await session.call_tool("get_weather", {"city": "Berlin", "units": "celsius"})
for block in result.content:
if block.type == "text":
print(block.text)
```
### Step 6: List and read resources
```typescript
// List all available resources
const resources = await client.listResources();
for (const resource of resources.resources) {
console.log(`- ${resource.uri}: ${resource.description}`);
}
// Read a specific resource
const content = await client.readResource({ uri: "config://app" });
for (const block of content.contents) {
if (block.mimeType === "application/json") {
console.log(JSON.parse(block.text as string));
}
}
```
**Python:**
```python
resources = await session.list_resources()
resource_content = await session.read_resource("config://app")
for item in resource_content.contents:
print(item.text)
```
### Step 7: List and use prompts
```typescript
const prompts = await client.listPrompts();
for (const prompt of prompts.prompts) {
console.log(`- ${prompt.name}: ${prompt.description}`);
}
const prompt = await client.getPrompt({
name: "code_review",
arguments: { language: "TypeScript", focus: "security" },
});
// Use prompt.messages with your LLM
```
### Step 8: Build an AI agent with tool use
```typescript
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic();
// Convert MCP tools to Anthropic format
const tools = toolsResponse.tools.map((tool) => ({
name: tool.name,
description: tool.description,
input_schema: tool.inputSchema,
}));
async function runAgent(userMessage: string) {
const messages = [{ role: "user" as const, content: userMessage }];
while (true) {
const response = await anthropic.messages.create({
model: "claude-opus-4-5",
max_tokens: 4096,
tools,
messages,
});
if (response.stop_reason === "end_turn") {
return response.content;
}
// Process tool calls
const toolUses = response.content.filter((b) => b.type === "tool_use");
const toolResults = await Promise.all(
toolUses.map(async (toolUse) => {
if (toolUse.type !== "tool_use") return null;
const result = await client.callTool({
name: toolUse.name,
arguments: toolUse.input as Record<string, unknown>,
});
return {
type: "tool_result" as const,
tool_use_id: toolUse.id,
content: result.content,
};
})
);
messages.push({ role: "assistant", content: response.content });
messages.push({ role: "user", content: toolResults.filter(Boolean) });
}
}
```
### Step 9: Clean up
```typescript
// Always disconnect when done
await client.close();
```
## Examples
### Example 1: List all capabilities of an MCP server
```typescript
const transport = new StdioClientTransport({
command: "npx",
args: ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
});
await client.connect(transport);
const [tools, resources, prompts] = await Promise.all([
client.listTools(),
client.listResources(),
client.listPrompts(),
]);
console.log(`Tools: ${tools.tools.map((t) => t.name).join(", ")}`);
console.log(`Resources: ${resources.resources.map((r) => r.uri).join(", ")}`);
console.log(`Prompts: ${prompts.prompts.map((p) => p.name).join(", ")}`);
await client.close();
```
### Example 2: Test a specific tool
```typescript
async function testTool(toolName: string, args: Record<string, unknown>) {
const result = await client.callTool({ name: toolName, arguments: args });
if (result.isError) {
console.error("FAIL:", result.content);
return false;
}
console.log("PASS:", result.content);
return true;
}
await testTool("query_database", { sql: "SELECT COUNT(*) FROM users", limit: 1 });
```
## Guidelines
- Always call `client.close()` after you're done to free resources
- Handle `isError: true` responses from `callTool` — these are tool-level errors, not exceptions
- Use `listTools()` at startup to dynamically discover available tools
- For long-running agents, reconnect if the server disconnects
- Validate tool arguments against the `inputSchema` before calling to catch errors early
- Use `StdioClientTransport` for local servers and `SSEClientTransport` for remote ones
- The `env` field in `StdioClientTransport` merges with the current process environment
Related in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.