43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { useState } from 'react';
|
|
|
|
/**
|
|
* Hook to call MCP tools from within an app
|
|
* In production, this would use window.mcpClient.callTool()
|
|
*/
|
|
export function useSmartAction() {
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const callTool = async <T = any>(toolName: string, args: any): Promise<T | null> => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
// In production MCP Apps, this would call:
|
|
// const result = await window.mcpClient.callTool(toolName, args);
|
|
|
|
// For now, we'll simulate API calls
|
|
const response = await fetch(`http://localhost:3100/mcp/tools/${toolName}`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(args)
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Tool call failed: ${response.statusText}`);
|
|
}
|
|
|
|
const result = await response.json();
|
|
return result as T;
|
|
} catch (err) {
|
|
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
|
|
setError(errorMessage);
|
|
return null;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return { callTool, loading, error };
|
|
}
|