1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! # Model Context Protocol (MCP) Core Library
//!
//! `mcp-core` is a Rust implementation of the Model Context Protocol (MCP), an open
//! protocol for interaction between AI models and tools/external systems.
//!
//! This library provides a comprehensive framework for building both MCP servers (tool providers)
//! and MCP clients (model interfaces), with support for:
//!
//! - Bidirectional communication between AI models and external tools
//! - Tool registration, discovery, and invocation
//! - Resource management
//! - Transport-agnostic design (supporting both SSE and stdio)
//! - Standardized request/response formats using JSON-RPC
//!
//! ## Architecture
//!
//! The library is organized into several main components:
//!
//! - **Client**: Implementation of the MCP client for connecting to servers
//! - **Server**: Implementation of the MCP server for exposing tools to clients
//! - **Protocol**: Core protocol implementation using JSON-RPC
//! - **Types**: Data structures representing MCP concepts
//! - **Transport**: Network transport abstraction (SSE, stdio)
//! - **Tools**: Framework for registering and invoking tools
//!
//! ## Usage
//!
//! For examples of how to use this library, see the `examples/` directory:
//! - `echo_server.rs`: A simple MCP server implementation
//! - `echo_server_macro.rs`: Using the `#[tool]` macro for simpler integration
//! - `echo_client.rs`: A client connecting to an MCP server
//!
//! ## Macros
//!
//! This library includes a set of utility macros to make working with the MCP protocol
//! easier, including helpers for creating various types of tool responses.
/// Creates a tool response with error information.
///
/// This macro generates a `CallToolResponse` containing a text error message
/// and sets the `is_error` flag to `true`.
///
/// # Examples
///
/// ```
/// use mcp_core::tool_error_response;
/// use anyhow::Error;
///
/// let error = Error::msg("Something went wrong");
/// let response = tool_error_response!(error);
/// assert_eq!(response.is_error, Some(true));
/// ```
/// Creates a tool response with text content.
///
/// This macro generates a `CallToolResponse` containing the provided text.
///
/// # Examples
///
/// ```
/// use mcp_core::tool_text_response;
///
/// let response = tool_text_response!("Hello, world!");
/// ```
/// Creates a text content object for tool responses.
///
/// This macro generates a `ToolResponseContent::Text` object with the provided text.
///
/// # Examples
///
/// ```
/// use mcp_core::tool_text_content;
///
/// let content = tool_text_content!("Hello, world!");
/// ```
/// Creates an image content object for tool responses.
///
/// This macro generates a `ToolResponseContent::Image` object with the provided data and MIME type.
///
/// # Examples
///
/// ```
/// use mcp_core::tool_image_content;
///
/// let image_data = "base64_encoded_data".to_string();
/// let content = tool_image_content!(image_data, "image/jpeg".to_string());
/// ```
/// Creates an audio content object for tool responses.
///
/// This macro generates a `ToolResponseContent::Audio` object with the provided data and MIME type.
///
/// # Examples
///
/// ```
/// use mcp_core::tool_audio_content;
///
/// let audio_data = "base64_encoded_audio".to_string();
/// let content = tool_audio_content!(audio_data, "audio/mp3".to_string());
/// ```
/// Creates a resource content object for tool responses.
///
/// This macro generates a `ToolResponseContent::Resource` object with the provided URI and optional MIME type.
///
/// # Examples
///
/// ```
/// use mcp_core::tool_resource_content;
/// use url::Url;
///
/// let uri = Url::parse("https://example.com/resource.png").unwrap();
/// let content = tool_resource_content!(uri, "image/png".to_string());
/// ```