@@ -5,12 +5,11 @@ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
5
5
import { z } from 'zod'
6
6
import axios , { AxiosInstance } from 'axios'
7
7
import dotenv from 'dotenv'
8
- import { CallToolResult } from '@modelcontextprotocol/sdk/types.js'
9
8
10
9
dotenv . config ( )
11
10
12
11
const envSchema = z . object ( {
13
- HABITIFY_API_KEY : z . string ( ) . min ( 1 ) ,
12
+ HABITIFY_API_KEY : z . string ( ) ,
14
13
} )
15
14
16
15
const env = envSchema . parse ( process . env )
@@ -21,11 +20,11 @@ const mcpServer = new McpServer(
21
20
version : '' ,
22
21
} ,
23
22
{
23
+ instructions : `API for Habitify habit tracking service` ,
24
24
capabilities : {
25
25
tools : { } ,
26
26
logging : { } ,
27
27
} ,
28
- instructions : `MCP server for Habitify API integration - track habits, manage mood logs, and automate habit tracking workflows directly from AI assistants like Claude and Cursor` ,
29
28
}
30
29
)
31
30
@@ -39,7 +38,7 @@ const logger = {
39
38
}
40
39
41
40
const apiClient : AxiosInstance = axios . create ( {
42
- baseURL : 'https://api.habitify.me ' ,
41
+ baseURL : '' ,
43
42
headers : {
44
43
Accept : 'application/json' ,
45
44
} ,
@@ -59,165 +58,10 @@ apiClient.interceptors.request.use(
59
58
}
60
59
)
61
60
62
- function handleResult ( data : unknown ) : CallToolResult {
63
- return {
64
- content : [
65
- {
66
- type : 'text' ,
67
- text : JSON . stringify ( data , null , 2 ) ,
68
- } ,
69
- ] ,
70
- }
71
- }
72
-
73
- function handleError ( error : unknown ) : CallToolResult {
74
- console . error ( error )
75
- logger . error ( 'Error occurred:' , JSON . stringify ( error ) )
76
-
77
- if ( axios . isAxiosError ( error ) ) {
78
- const message = error . response ?. data ?. description || error . message
79
- return {
80
- isError : true ,
81
- content : [ { type : 'text' , text : `API Error: ${ message } ` } ] ,
82
- } as CallToolResult
83
- }
84
-
85
- return {
86
- isError : true ,
87
- content : [ { type : 'text' , text : `Error: ${ error } ` } ] ,
88
- } as CallToolResult
89
- }
90
-
91
- mcpServer . tool ( 'get-journal' , `Get habit journal for a specific date` , { } , async ( args ) => {
92
- try {
93
- const response = await apiClient . get ( '/journal' , {
94
- params : args ,
95
- } )
96
- return handleResult ( response . data )
97
- } catch ( error ) {
98
- return handleError ( error )
99
- }
100
- } )
101
-
102
- mcpServer . tool ( 'post-logs-by-id' , `Add a habit log` , { } , async ( args ) => {
103
- try {
104
- const response = await apiClient . post ( '/logs/{habit_id}' , args )
105
- return handleResult ( response . data )
106
- } catch ( error ) {
107
- return handleError ( error )
108
- }
109
- } )
110
-
111
- mcpServer . tool ( 'delete-logs-by-id' , `Delete habit logs in date range` , { } , async ( args ) => {
112
- try {
113
- const response = await apiClient . delete ( '/logs/{habit_id}' , {
114
- params : args ,
115
- } )
116
- return handleResult ( response . data )
117
- } catch ( error ) {
118
- return handleError ( error )
119
- }
120
- } )
121
-
122
- mcpServer . tool ( 'delete-logs-by-id-by-id' , `Delete a specific habit log` , { } , async ( args ) => {
123
- try {
124
- const response = await apiClient . delete ( '/logs/{habit_id}/{log_id}' , {
125
- params : args ,
126
- } )
127
- return handleResult ( response . data )
128
- } catch ( error ) {
129
- return handleError ( error )
130
- }
131
- } )
132
-
133
- mcpServer . tool ( 'get-habits' , `Get all habits` , { } , async ( args ) => {
134
- try {
135
- const response = await apiClient . get ( '/habits' , {
136
- params : args ,
137
- } )
138
- return handleResult ( response . data )
139
- } catch ( error ) {
140
- return handleError ( error )
141
- }
142
- } )
143
-
144
- mcpServer . tool ( 'get-habits-by-id' , `Get habit details` , { } , async ( args ) => {
145
- try {
146
- const response = await apiClient . get ( '/habits/{habit_id}' , {
147
- params : args ,
148
- } )
149
- return handleResult ( response . data )
150
- } catch ( error ) {
151
- return handleError ( error )
152
- }
153
- } )
154
-
155
- mcpServer . tool ( 'get-areas' , `Get all areas` , { } , async ( args ) => {
156
- try {
157
- const response = await apiClient . get ( '/areas' , {
158
- params : args ,
159
- } )
160
- return handleResult ( response . data )
161
- } catch ( error ) {
162
- return handleError ( error )
163
- }
164
- } )
165
-
166
- mcpServer . tool ( 'get-moods' , `Get mood entries` , { } , async ( args ) => {
167
- try {
168
- const response = await apiClient . get ( '/moods' , {
169
- params : args ,
170
- } )
171
- return handleResult ( response . data )
172
- } catch ( error ) {
173
- return handleError ( error )
174
- }
175
- } )
176
-
177
- mcpServer . tool ( 'post-moods' , `Add mood entry` , { } , async ( args ) => {
178
- try {
179
- const response = await apiClient . post ( '/moods' , args )
180
- return handleResult ( response . data )
181
- } catch ( error ) {
182
- return handleError ( error )
183
- }
184
- } )
185
-
186
- mcpServer . tool ( 'get-notes' , `Get notes` , { } , async ( args ) => {
187
- try {
188
- const response = await apiClient . get ( '/notes' , {
189
- params : args ,
190
- } )
191
- return handleResult ( response . data )
192
- } catch ( error ) {
193
- return handleError ( error )
194
- }
195
- } )
196
-
197
- mcpServer . tool ( 'post-notes' , `Add note` , { } , async ( args ) => {
198
- try {
199
- const response = await apiClient . post ( '/notes' , args )
200
- return handleResult ( response . data )
201
- } catch ( error ) {
202
- return handleError ( error )
203
- }
204
- } )
205
-
206
- mcpServer . tool ( 'get-actions' , `Get available actions` , { } , async ( args ) => {
207
- try {
208
- const response = await apiClient . get ( '/actions' , {
209
- params : args ,
210
- } )
211
- return handleResult ( response . data )
212
- } catch ( error ) {
213
- return handleError ( error )
214
- }
215
- } )
216
-
217
61
async function main ( ) {
218
62
const transport = new StdioServerTransport ( )
219
63
await mcpServer . server . connect ( transport )
220
- logger . log ( 'Habitify MCP Server started' )
64
+ logger . log ( 'Habitify API MCP Server started' )
221
65
}
222
66
223
67
main ( ) . catch ( ( error ) => {
0 commit comments